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!

  • I guess this should read while ($the_query->have_posts()) : $the_query->the_post(); $count++; Feb 24, 2011 at 17:09

2 Answers 2


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.

  • 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, 2011 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'); Feb 24, 2011 at 18:14
  • Commented it out of where?
    – t31os
    Feb 24, 2011 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. Feb 25, 2011 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, 2011 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");

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.