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( 
                '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:


    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.


  • 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. Nov 13, 2013 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? Nov 13, 2013 at 13:52
  • Can you edit your question to include your attempt to use pre_get_posts? Nov 13, 2013 at 13:53
  • Sure, just give me a moment... Nov 13, 2013 at 13:54
  • 1
    By discussing it in chat if extensive discussion is needed?.. :)
    – Rarst
    Nov 13, 2013 at 14:58

1 Answer 1


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.

  • 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? Nov 13, 2013 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 :) Nov 13, 2013 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, 2013 at 15:01
  • +1. Second day in a row: I set 'em up, and @s_ha_dum knocks 'em down! Teamwork. :) Nov 13, 2013 at 15:17
  • Awesome, thank so much guys. I couldn't see it but had no idea of the need to put it there. Genius team yous make in that case ;) Nov 13, 2013 at 15:24

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.