I'm trying to display custom post types by excluding the sold ones but also by displaying them in alphabetical order from the county meta_key.

I'm trying to use pre_get_posts but I'm not sure that I doing it correctly

Here is my code on a page template:

$args = array(
    'post_type'      => 'property',
    'orderby'        => 'meta_value',
    'meta_key'       => 'random_775',
    'order'          => 'ASC',
    'posts_per_page' => 100,

$view_all_properties_sorted = new WP_Query( $args ); 

and here is the function in my functions.php file:

 add_action( 'pre_get_posts', 'sort_view_all_properties' );
 function sort_view_all_properties($view_all_properties_sorted) { 
    $view_all_properties_sorted->set('meta_key', 'sold');
    $view_all_properties_sorted->set('meta_value', 'true');
    $view_all_properties_sorted->set('meta_compare', '!=');

When I use this code, I'm getting a 404 error when I add this line:

$view_all_properties_sorted->set('meta_key', 'sold');

Is it because meta_key has already been set? Any suggestions on what I may be doing wrong?

  • Haven't you asked that already and today once? Have you deleted the other two questions (don't want to look that up now). Please just improve already existing questions instead of deleting and re-adding questions. Questions and answers serve a purpose as reference for later readers with the same problem as well. And people will stay back from answering questions when you delete them afterwards and kill their efforts/answers as well. Please keep that in mind. Thanks. Oh and please format your code in the best readable way.
    – kaiser
    Commented Sep 27, 2013 at 1:24
  • Sorry about that. I have deleted one but I thought this was a bit different. Should I delete this one as well? Thanks for the help. Commented Sep 27, 2013 at 1:31
  • Please, don't delete that one as well. You'd just have another person who wouldn't be willing to answer your questions any longer (see answer below). For the future, please just hit the "edit" link. After you edited your question, it will appear on the front page again.
    – kaiser
    Commented Sep 27, 2013 at 1:59

1 Answer 1


As you can read in Codex about orderby and the WP_Query, you can use multiple orderby values as well:

'orderby' => 'name meta_value'

simply by using a space in between. The available orderby values are listed in the Codex page.

About your question: By default it should already work with your query. If it doesn't, you'll have to add the output from posts_clauses, posts_request or use the pre_get_posts filter. More details in this answer.

  • Please let me explain myself a little better. Thanks for the links but I don't want to sort the order by multiple meta_values, I just want to sort by county (one value). I did not see anything in those links to address that issue. I'm guessing because I have two meta_key's it is not working as expected. Excluding post with 'sold' would not have anything to do with the orderby correct? Commented Sep 27, 2013 at 2:15
  • I fully understand you. The explanation about orderby shall just show you that you can sort by multiple things, not meta values (more info in Codex). The linked answer shows you how you can achieve sorting by multiple posts. It's not copy/paste, but a starting point.
    – kaiser
    Commented Sep 27, 2013 at 2:59

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.