0

This works if I replace $wpdb->posts with the literal table name in the database, but not if I try to switch it over to $wpdb->posts. I thought this is used to solve the databases being named wpgh, wpbs, wphh, or any other combination of wp and some characters.

$results = $wpdb->get_results( 'SELECT * FROM $wpdb->posts WHERE post_type = "page" OR post_type = "post" AND post_status = "publish"');

Am I doing something wrong or is there another way to get the posts table?

  • Your issue was single quotes, PHP variables only work inline inside double-quoted strings. – Rarst Dec 11 '16 at 19:47
0

Turns out this works.

global $wpdb;
    $postsTable = $wpdb->posts;
    $results = $wpdb->get_results( 'SELECT * FROM '.$postsTable.' WHERE post_type = "page" OR post_type = "post" AND post_status = "publish"');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.