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

Just wondered if someone could spare 5 mins to help me understand where I might be going wrong.

I have a set of custom fields for jobs - I would like to create two drop down menus as a quick search:

Categories for example - IT, Warehousing, Sales... Salary - 20-30k

I have cobbled together this query -

 $myquery = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value, post_status FROM $wpdb->postmeta, $wpdb->posts WHERE meta_key = %s AND post_status IN ('publish') ", $metakey));

With this query, it does populate a drop down list, however, it's still including 'drafts' - in the drop down.

So, how would I exclude 'draft pages' from populating the drop down?

Many thanks


share|improve this question
up vote 0 down vote accepted

I think you need a join.

$myquery = $wpdb->get_col(
        "SELECT DISTINCT meta_value FROM $wpdb->postmeta
         INNER JOIN $wpdb->posts ON $wpdb->postmeta.post_id = $wpdb->posts.ID
             meta_key = %s AND
             post_status IN ('publish')",
share|improve this answer
sorry for my delay, that is brilliant, thank you so much for that, it worked like a charm - I need to look into how to use JOIN! – Andy Jul 8 '13 at 13:03

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.