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 can't get the order of my posts (organized by custom tax terms) to come out right in my loop.

I made a custom post type of 'staff', in staff there is a custom taxonomy of 'staff-title'.

I added terms (similar to categories to keep the UI clean for the client) such as 'editor-in-chief', 'managing-editor', 'poetry-editor', etc. The idea is that the client can go into the 'staff' post type, create posts that are named after the employee and then check which position they hold and then the posts will be displayed in the proper order (displaying posts by running down the chain of command).

Here is the function creating my custom tax in functions.php:

function add_custom_taxonomies() {
    // Add new "Locations" taxonomy to Posts
    register_taxonomy('staff-title', 'staff', array(
        // Hierarchical taxonomy (like categories)
        'hierarchical' => true,
        // This array of options controls the labels displayed in the WordPress Admin UI
        'labels' => array(
            'name' => _x( 'Staff Titles', 'taxonomy general name' ),
            'singular_name' => _x( 'Title', 'taxonomy singular name' ),
            'search_items' =>  __( 'Search Titles' ),
            'all_items' => __( 'All Titles' ),
            'parent_item' => __( 'Parent Title' ),
            'parent_item_colon' => __( 'Parent Title:' ),
            'edit_item' => __( 'Edit Title' ),
            'update_item' => __( 'Update Title' ),
            'add_new_item' => __( 'Add New Title' ),
            'new_item_name' => __( 'New Title Name' ),
            'menu_name' => __( 'Staff Titles' ),
        ),
        // Control the slugs used for this taxonomy
        'rewrite' => array(
            'slug' => 'locations', // This controls the base slug that will display before each term
            'with_front' => false, // Don't display the category base before "/locations/"
            'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
        ),
    ));

}
add_action( 'init', 'add_custom_taxonomies', 0 );

Here is my WP_Query on the staff page...

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

$my_query = new WP_Query( $args );

The tax_query 'terms' => array(...) has the terms (staff positions) listed in the order they should be displaying but this isn't the case.

Visiting the staff page the posts are displaying in this order, which is the WRONG order:

Editor in Chief - Ching-in Chen
Managing Editor - Shanae Martinez
Production Manager - Matthew Morris-Cook
Nonfiction Editor - Kate Nesheim
Poetry Editor - C. McCallister Williams
Poetry Editor - Kara van de Graaf
Fiction Editor - Loretta McCormick
Fiction Editor - Mollie Boutell

Here's the CORRECT order just for reference (same order as the tax query terms array):

Editor in Chief - Ching-in Chen
Managing Editor - Shanae Martinez
Fiction Editor - Loretta McCormick
Fiction Editor - Mollie Boutell
Poetry Editor - C. McCallister Williams
Poetry Editor - Kara van de Graaf
Nonfiction Editor - Kate Nesheim
Production Manager - Matthew Morris-Cook

I put up the staff names as they are the title of the individual posts and I thought they might be a part of the problem.

Anyone have any ideas on how I could fix this? If you need more information let me know I'd be happy to provide. Thanks in advance!

share|improve this question

2 Answers 2

You can't oder by taxonomy term with WP_Query. You could do it with some custom SQL, but if the terms are hardcoded into the query anyway, just iterate over an array of terms in the order you want, then for each term loop over the posts within and check if it's associated to that term-

$args = array(
    'post_type' => 'staff',
    'posts_per_page' => '-1'
);
$my_query = new WP_Query( $args );
$terms = array(
    'editor-in-chief',
    'managing-editor',
    'fiction-editor',
    'poetry-editor',
    'nonfiction-editor',
    'production-manager'
);
foreach( $terms as $term ){
    while( $my_query->have_posts() ){
        $my_query->the_post();
        if( has_term( $term ) ){
            // output post
        }
    }
    $my_query->rewind_posts();
}
share|improve this answer
    
Oh I didn't realize that tax terms can't be ordered like this. Is there a better way for me to go about this completely than how I am? or would you suggest sticking with your answer? Also I think at some point the client wants to be able to add new terms (if new staff positions are created), it'd be nice if they could introduce themselves into the code automatically or with little trouble. –  user47713 Feb 17 at 18:21

It's already a hierarchical post type - no need for some extra meta value just for the order. You can simply add post type support for page-attributes when registering the post type. This will give you the "menu order" meta box that pages have per default.

Then simply order your query by it:

'orderby' => 'menu_order'
share|improve this answer

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.