Sorting WordPress Custom Taxonomy Archives

I recently created a custom post type [CPT] with an associated custom taxonomy on this blog to better manage my portfolio, and I thought I would share one little hoop I had to jump through to enable manual sorting of the post-type taxonomy archives.

Previously, long before migrating to WordPress, my portfolio was a page with an image and a paragraph or two for each project I wanted to highlight, all just maintained manually, with new items added to the top. After a decade, then more, of freelancing the list was getting pretty long, and because of how the Web has evolved, the tools and libraries available to a developer to deal with that evolution, and my own evolving skills and interests, the list needed some finer-grained controls to accurately communicate the scope of my work, an accuracy that’s important to help potential clients decide if I am a good fit for their project. (I am, almost always. You should hire me to do something.)

Creating the CPT was straightforward, thanks to excellent documentation and the fact that I’ve done it several times before, as was creating the custom taxonomy to assign skills/technology to portfolio projects, though I’m less experienced with that. I also wanted to provide an archive of each skill/technology in the taxonomy so that, for instance, if a project has “PHP” among the list of skills/tech used, clicking on “PHP” would take the user to a page containing all portfolio items that used “PHP”. Just like you often see with tags.

Initially, that was a walk in the park, but things got hard when I tried to sort the items according to the manual sort order set for each post. I eventually found what I needed in the documentation, and in the hopes of helping someone else out, here’s the ridiculously easy solution I ended up with (an explanation follows):

* Displays archive list of "Skills" taxonomy, for "Portfolio" post-type.


// Uses the default query settings except changes to order and quantity per page.
global $wp_query;

$args = array_merge( $wp_query->query_vars ,
‘orderby’ => ‘menu_order’,
‘order’ => ‘DESC’,
‘posts_per_page’ => -1 )

$term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );
echo ‘<h1>Skill Archive: ‘ . $term->name . ‘</h1>’;

while ( have_posts() ) {
    get_template_part('content', 'portfolio');

<?php get_sidebar();

From previous experience, I knew that setting “orderby” to “menu_order” and “order” to “DESC” would sort the items the way I wanted, but my first crack at creating a new query using query_posts() didn't return the necessary post meta-data for the Skills taxonomy.

What I needed was the native taxonomy database query, which mostly worked just fine, with slight modifications to the sort order. The solution was the PHP function array_merge() starting on line 11, which produces a union of the two arrays, with the returned array overwriting duplicate keys (in my case “orderby” and “order”) with the corresponding values in the array used as the function’s second argument.

I could have used query_posts() and written a full arguments array, but since the default db query already provided everything I needed, a slight change was much more efficient, and more robust as well, from a maintenance standpoint.