WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using following code in the theme files to get some output from the DB:

<?php $querystr = "SELECT DISTINCT meta_value FROM $wpdb->wp_postmeta WHERE 'meta_key' LIKE 'movie_name' ORDER BY meta_value ASC";
$movie_names =  $wpdb->get_results($querystr, OBJECT);
echo sizeof($movie_names);
<?php foreach ( $movie_names as $movie_name ){ ?>
   <li><?php echo $movie_name->meta_value; ?> </li>
<?php } ?>

Above code's result is "0" at the frontend. But when I am running the same query i.e.

SELECT DISTINCT meta_value FROM wp_postmeta WHERE 'meta_key' LIKE 'movie_name' ORDER BY meta_value ASC

in DB then it's returning 6 rows. Please help me in finding the problem in the code.

share|improve this question
up vote 4 down vote accepted

$wpdb->wp_postmeta is wrong. Always use just $wpdb->postmeta – without further prefixes.


global $wpdb;

$querystr = "
    SELECT DISTINCT meta_value 
    FROM $wpdb->postmeta 
    WHERE meta_key LIKE 'movie_name' 
    ORDER BY meta_value ASC

$movie_names = $wpdb->get_results( $querystr, OBJECT );

if ( ! $movie_names ) {
    $wpdb->print_error(); // Get the last error message for debugging
else {
    // Do something awesome with the movies
share|improve this answer
Thanks Toscho. Its working now with some little modification. – Akhilesh Apr 24 '13 at 16:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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