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.

  • 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, 2013 at 10:03

1 Answer 1


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.

  • 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, 2013 at 4:32
  • 1
    That looks about right then.
    – s_ha_dum
    Jul 5, 2013 at 4:36
  • 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, 2013 at 5:09
  • 1
    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, 2013 at 5:23
  • Thanks; I see what your saying, it seems the terms can be a bit ambiguous depending on how one sets up their wordpress, static front or not, etc... Thank you for the heads up.
    – Jacob
    Jul 5, 2013 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.