$wpdb->get_results(…) returns empty array despite correct query

Use $wpdb->show_errors( true ) before the query and see what error comes back.

My problem (because I’ve experienced the same thing) was that I should use $wpdb->posts instead of wp_posts inside the query. The prefix of the tables can change from WP installation to installation or even in the same installation depending on the time (e.g. an admin can change the prefix whenever he likes). So, one should write the query like that:

$query = "select <stuff here> from $wpdb->posts where <stuff here>";

Leave a Comment