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'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?

share|improve this question
    
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 Sep 27 '13 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. –  graphical_force Sep 27 '13 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 Sep 27 '13 at 1:59
    
Ok. Thanks for the help. :) –  graphical_force Sep 27 '13 at 2:16
add comment

1 Answer

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.

share|improve 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? –  graphical_force Sep 27 '13 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 Sep 27 '13 at 2:59
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.