0

I'm querying a custom post type and displaying all posts by terms (basically posts by category) as follows, everything is working fine EXCEPT for some reason no matter how I write the tax_query array, I can't seem to get the order of the terms to change (ASC and DESC don't change anything).

Can anyone see where I might have gone wrong here?


$temp_query = $wp_query;

$custom_terms = get_terms('instruction_categories');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(
        'post_type' => 'instruction-sheets',
        'orderby' => 'name', // order of the products
        'order'  => 'ASC',
        'hide_empty' => 1,
        'tax_query' => array(
            array(
                'taxonomy' => 'instruction_categories',
                'field' => 'slug',
                'terms' => $custom_term->slug,
                'hide_empty' => 1,
                'orderby' => $custom_term->name,
                'order' => 'ASC', // switching to DESC should reverse order, but doesnt
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo 'Region '.$custom_term->name.'';

        while($loop->have_posts()) : $loop->the_post();

0

For anyone having the same issue, here's how I ended up solving this. I installed the WP Term Order plugin and then used the following code, the magic happens in the 'orderby' => 'order' line where the 'order' is being pulled from the drag and drop menu order functionality in the admin added by the plugin.


$custom_terms = get_terms('instruction_categories');

foreach($custom_terms as $custom_term) {
wp_reset_query();

$args = array(
    'post_type' => 'instruction-sheets',
    'orderby' => 'meta_value',
    'order'  => 'ASC',
    'hide_empty' => 1,
    'meta_key' => $custome_term->slug,
    'tax_query' => array(
        array(
            'taxonomy' => 'instruction_categories',
            'field' => 'slug',
            'terms' => $custom_term->slug,
            'hide_empty' => 1,
            'orderby' => 'order',
            //  'order' => 'DESC',
        ),
    ),
 );


 $loop = new WP_Query($args);
 if($loop->have_posts()) {

 while($loop->have_posts()) : $loop->the_post();

Then enter whatever code you need to show for parts of your post content, and then don't forget to close the whole thing out with:


    endwhile; 
    }
}

0

I believe your query arguments need revision. The value for orderby should be meta_value or meta_value_num for a numeric sort.

You also need a meta_key set in your arguments array and in your case that should be set to the value of $custom_term->slug.

Untested arguments for your query:

$args = array(
    'post_type' => 'instruction-sheets',
    'orderby' => 'meta_value', // Change #1
    'order'  => 'ASC',
    'hide_empty' => 1,
    'meta_key' => $custome_term->slug,
    'tax_query' => array(
        array(
            'taxonomy' => 'instruction_categories',
            'field' => 'slug',
            'terms' => $custom_term->slug,
            'hide_empty' => 1,
        ),
    ),
 );

WP Codex reference for sorting: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  • Thanks for the response, sadly, this still didn't solve the issue, though it may still have been a contributor, I've altered my code with your 'meta_value' tip. I've solved this in a round-about way by using a plug-in to add a menu-order column to the terms list in the admin which is drag and drop, then by simply using 'orderby' => 'order', the list will reflect the admin's drag and drop order. Thanks again. – TheSurfer Apr 3 at 22:06
  • Roger that - I think I see why. I inadvertently left the order and orderby args in the tax query array which would foul the query. Edited above if you want to try it. – jdm2112 Apr 3 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.