0

I'm looping through a series of custom post types and then grouping those by a taxonomy term. That is working great, however I have not been able to order these groupings by term_id.

I used a var_dump to check the term_id and they are as expected. In fact, no orderby parameters are working at all - they are always ordered by the taxonomy term title. My taxonomy in this case is issue_area with terms like 'Human Services', 'Education' and so on.

What am I missing here?

Here is the loop I am using:

<?php

$terms = get_terms('issue_area'); // this is my custom taxonomy
foreach( $terms as $term ):
$termed =  preg_replace('/\d/', '', $term->name ); // split out numbers
?>                          

<h3 class="expert-term"><?php echo $termed; // Print the term name ?></h3>

<?php var_dump($term->term_id); ?> <?php // this returns an int as expected ?>

    <ul class="experts">

    <?php $posts = get_posts(array(
     'post_type' => 'uwmc_experts', // my cpt
     'taxonomy' => $term->taxonomy,
     'term' => $term->slug,
     'orderby' => $term->term_id, // tried: taxonomy_term_id
     'order' => 'ASC', // tried DESC - no change
     'numberposts' => -1,
    ));

    foreach($posts as $post): 
     setup_postdata($post); 
    ?> 

    <?php if( have_rows('experts') ): ?> <?php // acf repeater ?>

    <?php while( have_rows('experts') ): the_row(); 

        $image = get_sub_field('expert_photo');
        $content = get_sub_field('expert_bio');
        $areas = get_sub_field('areas'); ?>

        <li class="expert">

        <?php // rest of repeater loop stuff here ?>
4
  • I don't think you can order posts by term groupings like you expect without some custom SQL. Here's a list of orderby parameters. If you take a look at this answer it seems like doing so could get complicated.
    – Howdy_McGee
    Commented Apr 28, 2016 at 15:21
  • @Howdy_McGee the var_dump returns the term_id which is a valid return value. See the Codex for orderby values with [get_term] (codex.wordpress.org/Function_Reference/get_term). It seems like there should be a way without using any SQL queries but maybe not.
    – joshuaiz
    Commented Apr 28, 2016 at 18:37
  • The other thing that should be reiterated is that NO orderby values/parameters seem to be working.
    – joshuaiz
    Commented Apr 28, 2016 at 18:37
  • I'm also not 100% sure that taxonomy is valid anymore as a parameter in queries either. Maybe try turning that into a tax_query instead or taxonomy_slug => term_slug ( though I've had issues with this in the past ) as seen in the linked example. Maybe at that point you'll be able to orderby => 'title'.
    – Howdy_McGee
    Commented Apr 28, 2016 at 18:43

1 Answer 1

1

Instead of trying to do this through the orderby parameter, I went a different direction.

Logically, the orderby parameter above only applies to the posts within each grouping, not the taxonomy term grouping itself so it won't work for this.

What I ended up doing was adding 01, 02, 03, etc. to the taxonomy term name itself, then stripped that out using PHP. Thus I had terms like:

01 Human Services
02 Education 
03 Financial Stability

I numbered these in the order I wanted as they won't be changing dynamically. Then I used the following to strip out the numbers from the term and echo the updated terms:

<?php
// Output all Taxonomies names with their respective items
$terms = get_terms('issue_area');
foreach( $terms as $term ):
$termed =  preg_replace('/\d/', '', $term->name );
?>                          

<h3 class="expert-term"><?php echo $termed; // Print the term name ?></h3>

Obviously a lot easier than a complex SQL query and gives me the results that I need.

Here is a live example of this: http://pressroom.uw-mc.org/meet-our-experts/

My original question has also been updated with my answer.

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.