Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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.
http://codex.wordpress.org/Function_Reference/query_posts#Custom_Field_Parameters

Info on order by to, since i added that into the above to.. ;)
http://codex.wordpress.org/Function_Reference/query_posts#Order_.26_Orderby_Parameters

..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

endwhile;

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
show 3 more comments

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
add comment

Your Answer

 
discard

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.