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 have added a custom field to some posts called 'frontpagerank' The plan is to order the posts by this value but first I just want to filter out any that don't use a front page rank. I have achieved this by putting the relevant posts into another array. But what to do next?

Also tried a query: $the_query = new WP_Query(array( 'meta_key' => '0', 'meta_value' => '44' ));

while ($the_query->have_posts()) : $query->the_post(); $count++;

This just does nothing. This code is hard to debug!

share|improve this question
I guess this should read while ($the_query->have_posts()) : $the_query->the_post(); $count++; – JohnnyBizzle Feb 24 '11 at 17:09
up vote 0 down vote accepted

I have added a custom field to some posts called 'frontpagerank'

Then shouldn't the WP_Query args reference that key then, eg.

'meta_key' => 'frontpagerank'

If i follow, you want to check for posts that have this key, and you're expecting a numeric value, so i'll naturally assume you don't want posts with this key(but an empty value).

$the_query = new WP_Query(array( 'meta_key' => 'frontpagerank', 'meta_value' => '', 'meta_compare' => '!=', 'orderby' => 'meta_value_num' ));

Or if you specifically want to check for posts with that meta_key where the value is above say 0, you could do..

$the_query = new WP_Query(array( 'meta_key' => 'frontpagerank', 'meta_value' => '0', 'meta_compare' => '>', 'orderby' => 'meta_value_num' ));

You can read info on the meta parameters here.

Info on order by to, since i added that into the above to.. ;)

..any parameters you see listed for query_posts can be used inside WP_Query

Then all you need do is loop over that data like you had earlier..

// Note i made the correction you commented on, yes that's need to match
while( $the_query->have_posts() ) : $the_query->the_post();

   // do whatever


Hope that helps.

share|improve this answer
Though really i should have used meta_query instead here, i've no experience using it yet, so i've shown how to use the pre 3.1 method. – t31os Feb 24 '11 at 17:15
Ok thanks. Seems to be working ok, but only when I unstuck the sticky posts. Is there a way to stop sticky posts? I commented out this line // $sticky=get_option('sticky_posts'); – JohnnyBizzle Feb 24 '11 at 18:14
Commented it out of where? – t31os Feb 24 '11 at 23:12
I am using a theme called IsoTherm : [link]bizzthemes.com/themes/isotherm Without posting the whole source from the page, I can tell you that is the only reference to sticky in the source code. – JohnnyBizzle Feb 25 '11 at 16:52
I don't need to see all the code, i'm trying to work out what your question is regarding stickies, i don't follow what it is your're asking. Do you want to exclude sticky posts from the query? – t31os Feb 25 '11 at 18:37

Haven't tried it, but this seems like it should work:

$recent = new WP_Query("meta_key=your-custom-field&orderby=meta_value");
share|improve this answer
It works, thanks. – JohnnyBizzle Feb 25 '11 at 16:55
You are very welcome. Could you throw a "checkmark" on this answer? It helps my ranking! Thanks :) – Tom Mar 1 '11 at 2:09

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.