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 am trying to create theme pages for custom taxonomy terms in which the archives are ordered by a custom meta field.

I can do this with a new wp_query as follows:

        <?php $directory_archive_query = new WP_Query( 
            array( 
                'post_type' => 'directory',
                'posts_per_page' => 200,
                'meta_key' => 'surname',
                'orderby' => 'meta_value',
                'order' => 'ASC' ) ); ?>    

            <?php while($directory_archive_query->have_posts()) : $directory_archive_query->the_post(); ?>

However, I need this to work on the taxonomy-custom.php page so that each individual taxonomy term's archive page is listed according to the custom meat field (called 'surname'). If I use the wp_query, then of course that doesn't work, because it no longer shows results for each specific term.

I have also tried to do this using pre_get_posts, but can't make that work either. What I am expecting to happen is that all posts listed on the term archive page will be listed in ascending order by the surname meta key. That is not happening. Instead, they are just listing in the same order as if there were no pre_get_posts there. The code I have used is as follows:

<?php 

    function customize_customtaxonomy_archive_display ( $query ) {
        if (($query->is_main_query()) && (is_tax('services')))

        $query->set( 'post_type', 'directory' );                 
        $query->set( 'posts_per_page', '200' );
        $query->set( 'meta_key', 'surname' );           
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'ASC' );
    }

     add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );

?>

Really hoping that someone can help.

Thanks for your time.

Andrew.

share|improve this question
    
Creating custom, secondary queries to replace the main query is the wrong approach. You need to use pre_get_posts to modify the main query instead. –  Chip Bennett Nov 13 '13 at 13:51
    
Thanks @ChipBennett. I have tried doing that too, but I can't get it to work. Is there any way you can show me how the code would work in this example please? –  Andrew Brown Nov 13 '13 at 13:52
    
Can you edit your question to include your attempt to use pre_get_posts? –  Chip Bennett Nov 13 '13 at 13:53
    
Sure, just give me a moment... –  Andrew Brown Nov 13 '13 at 13:54
1  
By discussing it in chat if extensive discussion is needed?.. :) –  Rarst Nov 13 '13 at 14:58
show 20 more comments

1 Answer

up vote 1 down vote accepted

The pre_get_posts filter is immediately before the loop begins in taxonomy-services.php

That is too late. The main query runs long before your template loads. Move your pre_get_posts filter to your theme's functions.php, or a plugin or MU-Plugin file, and you should see the difference.

share|improve this answer
    
Is this for sure? I have never seen any advice on this anywhere before. Surely I only need it to work on this template file? –  Andrew Brown Nov 13 '13 at 14:50
    
Man, you are a saviour! I have moved it to functions.php and it's now working beautifully! Thanks so much for your time :) –  Andrew Brown Nov 13 '13 at 14:58
    
Yes, this is for sure. You have to add the hook before the query runs and the main query runs before the template loads-- it is what determines what template to load. You can add a pre_get_posts action just before new WP_Query objects that you create. –  s_ha_dum Nov 13 '13 at 15:01
    
Thanks so much :) –  Andrew Brown Nov 13 '13 at 15:03
    
+1. Second day in a row: I set 'em up, and @s_ha_dum knocks 'em down! Teamwork. :) –  Chip Bennett Nov 13 '13 at 15:17
show 1 more 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.