I'm trying to select the post_title from wp_posts with a query which I did using this method:

     global $wpdb;
     $test = $wpdb->get_var( 
        $wpdb->prepare(" SELECT post_title FROM {$wpdb->wp_posts}" )
     echo "$test";

EDIT: It now gives me:

Notice: Undefined property: wpdb::$wp_posts in /../../../../wp-includes/wp-db.php on line 566

Warning: Missing argument 2 for wpdb::prepare(), called in /../../../../wp-content/themes/pms72/page-home.php on line 73 and defined in /../../../../wp-includes/wp-db.php on line 992
  • 1
    $row is undefined, it hasn't been given a value, so it's incorrect to try to retrieve a post title, as it's not an array of values, it's empty/null/void. Its like buying a brand new notebook then wondering why all the pages are blank.
    – Tom J Nowell
    Commented Mar 3, 2014 at 16:09
  • Yea I noticed I just forgot to change this page, it was really stupid of me but regardless I changed it up now and it's telling me $wpdb is undefined.
    – David H
    Commented Mar 3, 2014 at 16:17
  • You need to declare $wpdb as a global before using it. Commented Mar 3, 2014 at 16:18
  • I have. global $wpdb right?
    – David H
    Commented Mar 3, 2014 at 16:19

2 Answers 2


First, it's not $wpdb->wp_posts, it's just $wpdb->posts.

Second, you're using prepare() incorrectly. You use prepare when you have some variable data that you need to safely insert into the query string, not otherwise. Using prepare on a known string does nothing and throws a warning.

For example:

$data = 'example string'
$query = $wpdb->prepare("SELECT post_title FROM {$wpdb->posts} where post_title = %s", $data);

And then $query will be the SQL string that you're able to safely send to $wpdb->query. The string gets escaped, quoted, and inserted where that %s is.

If you don't have any variable data that needs to be prepare'd for inserting into the query, then there's no point in calling prepare.

For your case, since you're not putting any variables into the SQL, just skip the prepare.

$test = $wpdb->get_var( "SELECT post_title FROM {$wpdb->posts}" );
  • This worked! By the way about the prepare I forgot to remove it and I remember why it's there. If I reckon correctly I had written posts there as well as I have no idea why` wp_posts` was written there I was pretty sure I removed the wp part because I noticed it didn't work. Regardless I learned something and solved my problem thanks :).
    – David H
    Commented Mar 4, 2014 at 14:36

You should be able to get the posts table name using.

$tableName = $wpdb->prefix . 'posts';

So, your script would become:

global $wpdb;
$tableName = $wpdb->prefix . 'posts';
$test = $wpdb->get_var( 
   $wpdb->prepare(" SELECT post_title FROM {$tableName}" )
  • I get a warning: Warning: Missing argument 2 for wpdb::prepare(),
    – David H
    Commented Mar 4, 2014 at 14:32
  • As per Otto's response, prepare is being used incorrectly here - apologies, I didn't update my example to take this into account. Commented Mar 4, 2014 at 14:44
  • It's all good we learned something from it :)
    – David H
    Commented Mar 4, 2014 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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