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 am trying to sort my home page posts by a custom meta value. I've been told I should use pre_get_posts but I seem to not be implementing it correctly.

This is what I am doing:


function alter_query($query){

      if( $query->is_home() ){
        $query->set( 'meta_key', 'key name' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
     return $query; 

When I use the above function, the homepage query is not returning all the posts and not ordering them properly; I am trying to figure out if it I am going about this the correct way.

share|improve this question
Might wanna chuck in a $query->is_main_query() in there to, just to be sure you target only the intended main query(else your above code could catch nav menu queries to, as but one example). – t31os Jul 5 '13 at 10:03
up vote 2 down vote accepted

You probably also need:

$query->set( 'ignore_sticky_posts', true );

That will prevent sticky posts from shuffling to the top.


$query->set( 'posts_per_page', -1 );

If you are getting too few posts. -1 means "all". You can use that to change to a number other than what is set in the backend.

A couple of notes:

  1. $query->set( 'meta_key', 'key name' ); will limit the results to posts with that meta_key
  2. If your meta_value for the key name isn't really a number the order isn't going to work correctly. Punctuation and letters will mess this up.
  3. is_home can be peculiar. Make sure it does what you need it to do.

Beyond that, you will need to explain "not returning all the posts and not ordering them properly" in better detail.

share|improve this answer
Thanks for the reply. I just wanted to make sure that this was going about it proper. About the "not returning all the posts..." I actually think that's an issue with Jetpack's Infinite Scroll module; normal navigation seems to return the posts properly. So, as long as I am going about this correctly, I can move on to debugging the infinite scroll problem. – Jacob Jul 5 '13 at 4:32
That looks about right then. – s_ha_dum Jul 5 '13 at 4:36
Ok, cool; thanks again:) – Jacob Jul 5 '13 at 4:45
Quick follow up if you have a spare moment; when you say is_home can be peculiar; is there a better conditional tag to make sure the query I am adjusting is the main page (is_front_page or something else)? TY – Jacob Jul 5 '13 at 5:09
is_front_page can be peculiar too. :) It isn't that the function behave unpredictably. It is that they don't necessarily behave the way you might guess from the names and certain backend settings alter their behaviors. Just read the Codex entries for the function very carefully. – s_ha_dum Jul 5 '13 at 5:23

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.