Get Previous & Next posts by Post ID

Take a look at get_previous_post() and get_next_post() and you’ll see they both use the get_adjacent_post() to find the previous or next post. Let’s say you want to fetch the ID of the immediately previous post based on the current post’s ID. This is what you’d do: function get_previous_post_id( $post_id ) { // Get a global … Read more

Retrieve featured image

The featured image attachment id is stored in the post_meta table, with the meta_key _thumbnail_id. Use that post id to find the post meta with meta_key _wp_attachment_metadata. The value of that is a serialized array with keys thumb, file, width, height, sizes (an array), and image_meta (an array). The file value is relative to the … Read more

Connect to database using wordpress wp-config file

Using the defines the user sets in wp-config: mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); EDIT: Since your script is outside the WordPress environment, what you want to do is initiate it before using the defines in wp-config. require_once(‘./path/to/the/wp-config.php’); mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

WordPress Database lost auto increment

I had a similar issue, I solved it and since this comes up high on Google for what I was looking for it may help others. I migrated several WordPress databases from AWS RDS MySQL to MySQL running on an EC2 instance, using the database migration service. What I didn’t know is it doesn’t copy … Read more

dbDelta support for FOREIGN KEY

Do I really have to live with that until dbDelta supports FOREIGN KEY? Quite frankly, yes. But that’s the beauty of open source – anyone is welcome to post a patch! However, expanding it to cover other aspects of schema design would almost certainly incur unwanted complexity & heighten the possibility of failure – something … Read more

How to fetch Data in WordPress using MySQLi or $wpdb

To fetch data from database table <?php $results = $wpdb->get_results( “SELECT * FROM $table_name”); // Query to fetch data from database table and storing in $results if(!empty($results)) // Checking if $results have some values or not { echo “<table width=”100%” border=”0″>”; // Adding <table> and <tbody> tag outside foreach loop so that it wont create … Read more

Mysqldump add drop table?

It only affects the output of your MySQL dump in the file that is created. It isn’t necessary. It is just there so that if you import the created dump file into a database that already has a table with the same name, it will drop that table and then add the new table in … Read more