0

I want my tax_query terms to display posts in the order I have them listed in the array below but it is not following the order listed.

$args = array(
    'post_type' => 'staff',
    'posts_per_page' => '-1',
    'tax_query' => array(
            array(
                'taxonomy' => 'staff-title',
                'field' => 'slug',
                'terms' => array(
                                'editor-in-chief',
                                'managing-editor',
                                'fiction-editor',
                                'poetry-editor',
                                'nonfiction-editor',
                                'production-manager'
                            )
            )
        )
);
$my_query = new WP_Query( $args );

    if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();


                echo'<div class="loop four columns">';
                    echo'<div class="column-nest">';
                        echo'<h5 class="staff-title big-top-space">'; the_field('staff_title'); echo'</h5>';
                        echo'<h6 class="staff-name  bottom-space">'; the_field('staff_member_name'); echo'</h6>';               
                    echo'</div> <!-- column-nest -->';
                    echo'<figure class="staff-photo-border">';
                        echo'<img class="column-nest" src="'; the_field('staff_member_photo'); echo'" alt="photo of the_field(\'staff_member_name\');" />';
                    echo'</figure> <!-- staff-photo-border -->';
                echo'</div> <!-- loop four columns -->';


    the_content();
 endwhile; endif; ?>

instead it displays posts in this order:

Fiction Editor
Fiction Editor
Poetry Editor
Poetry Editor
Nonfiction Editor
Production Manager
Managing Editor
Editor-in-Chief

2
  • Do you expect posts to be ordered in the order that you provide in the terms array?
    – s_ha_dum
    Commented Feb 18, 2014 at 19:31
  • 1
    With Wordpress I've come to expect nothing - but yes that's what I was hoping/searching for. Commented Feb 18, 2014 at 19:34

1 Answer 1

0

tl;dr;

This is not possible with tax_query.

A tax_query is a restriction of the results delivered, not used to order posts. You use the Order & Orderby Parameters for that.

A little excursion into statistics

This has to do with statistics and mathematics, as a taxonomy is a nominal scale, meaning it is just a classification, where every item is on the same level. Like the color of an eye - you can't say which is better just with this data.

To order data you have to have some kind of a comparison between the data. This is called the ordinal scale, (yeah, the word order is in ordinal), for example 3 is bigger than 2 is bigger than 1, therefor you can order them with the smallest or the biggest first (ASC and DESC). Please note that title also follows an ordinal scale, but not by comparison of the content, but by the scale of the alphabet.

*to be complete, there are also the interval scale and the ratio scale, but those have nothing to do with the WordPress Query.

Back to WordPress

Now as we learned that you can not order a taxonomy (without Metadata), you can just use it to define a group of characteristics the result should have.

In the tax_query you define a set of characteristics a post should have, nothing more. Please not that a set could also be a restriction like NOT IN.

After you defined the restrictions with tax_query (and, to be fair, meta_query does the same thing with different data), you can tell WordPress how to order your results, based on a measurable variable (be it an integer, alphabetical or any other ordinal scale).

This happens by adding

    'order' => 'ASC', // Order Ascending
    'orderby' => 'title', // Order by Title

to your $args array.

a possible solution

One thing you could do, however, is querying your restrictions in sequence, not all together. Please note that this approach is heavier for the database.

In my example I use an array to define the order of my taxonomies, and loop through this array, querying every taxonomy on it's own.

By putting the taxonomies into an ordered array, I added a ordinal capability to it. The taxonomy itself is still nominal.

$taxonomies = array(
    'editor-in-chief',
    'managing-editor',
    'fiction-editor',
    'poetry-editor',
    'nonfiction-editor',
    'production-manager'
);
foreach( $taxonomies as $thistaxonomy ) {

    $args = array(
        'post_type' => 'staff',
        'posts_per_page' => '-1',
        'tax_query' => array(
            array(
                'taxonomy' => 'staff-title',
                'field' => 'slug',
                'terms' => array(
                    $thistaxonomy
                )
            )
        )
    );
    
    /* All your query and magic down there */

}

Hope this works for you!

2
  • Thanks @fischi, works great! I appreciate the explanation too. BTW, not to be a nuisance, just for the sake of best practices, should I avoid setting this up the way I did, is there perhaps better way to have gone about it? Commented Feb 18, 2014 at 20:23
  • Not if you want to have them displayed like you do. at least none I can think of ;)
    – fischi
    Commented Feb 18, 2014 at 20:26

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.