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

How do I query_posts and only show results if a custom field is not empty or has a value.

I want to put in a url in a custom field and only show these pages if there is a url?

current code but I can't figure out the rest:

$args = array( 'posts_per_page' => '10', 'post_type' => 'programmes', 'orderby' => 'meta_value_num', 'meta_key' => 'popularityfig', 'order' => 'DESC', );

share|improve this question
up vote 2 down vote accepted

Try this code:

$args = array(
'posts_per_page' => '10',
'post_type' => 'programmes',
'meta_key' => 'popularityfig',
'meta_value' => '',
'meta_compare' => '!=',
'order' => 'DESC'

There're 2 arguments you might want to note in the code: meta_value and meta_compare. Using meta_compare with operator != will exclude posts with empty meta value.

share|improve this answer
Thanks, I can see that on codex now?? Why I couldn't see it before I'm not sure!! Thanks again. – erichmond Feb 24 '11 at 10:08

It's not clear if the answer above actually worked for you @erichmond, but it definitely did not for me. Found a solution though. See: How can I show posts only if meta_value is not empty

share|improve this answer

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.