• Resolved Freelancealot

    (@freelancealot)


    Hi,

    I am building a business listings site using custom post type (goto_listing) and custom taxonomy (business-categories).

    I have set up templates for the taxonomy archive pages for each (parent) term (e.g. taxonomy-business-categories-fabrication.php)

    On these archives I want:

    Under the parent term title (eg Fabrication) I want the child terms as h3 headings and the listings (custom posts) underneath.

    I have achieved this but currently only the post titles are in alphabetical order. Their headings (child terms) appear to be ordered by the date (for some reason).

    Here is the code that gets the current parent term it’s child terms and the related posts:

    <?php
        $term_slug = get_query_var( 'term' );
        $taxonomyName = get_query_var( 'taxonomy' );
        $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
        $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
        foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
            $wpq = array (
            'post_type' => 'goto_listing',
            'taxonomy'=>$taxonomyName,
            'term'=>$term->slug,
            'posts_per_page'=>'20',
            'orderby'=>'title',
            'order'=>'ASC'
            );
            $query = new WP_Query ($wpq);
            echo "<h3>$term->name</h3>";
            ?>
    
    	<ul class="goto-leaders">
            <?php
            if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
    
    <li><span><?php the_title();?></span><span><?php echo CFS()->get( 'goto_telephone' ); ?></span></li>
            <?php endwhile;  wp_reset_postdata(); ?>
    </ul>

    The result is:

    Child Term B
    A Business
    B Business
    C Business

    Child Term A
    A Business
    B Business
    C Business

    Child Term C
    A Business
    B Business
    C Business

    I need it to be:

    Child Term A
    A Business
    B Business
    C Business

    Child Term B
    A Business
    B Business
    C Business

    Child Term C
    A Business
    B Business
    C Business

    Basically I need an orderby ‘term name’ order ASC but after many many many hours I have been unsuccessful.

    It seems like such a simple task, maybe I’m overcomplicating it?

    Thanks for any help you can give.

    Cheers,
    Tracy

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Unfortunately, it’s not possible to order by taxonomy term names using WP_Query. The usual workaround is to alter the ORDER BY clause of the SQL query to get what you want. However, the terms table which contains the term names to order by is not even joined into a normal taxonomy query, so you would need to alter the join clause as well to bring in the terms table.

    There’s individual filters for each clause, as well as one where all the components are passed to your callback. Look at /wp-includes/query.php beginning line 3299 (v4.5) for where the various filters are applied. There’s also the ‘posts_request’ filter where you can edit or completely replace the full SQL query with what ever you want.

    Thread Starter Freelancealot

    (@freelancealot)

    Hi bcworkz,

    Is there another way to achieve what I need in the WordPress loop to get the terms sorted by ‘name’.

    You can see a page in action here: https:// www. gotoalmanac. com/business-categories/more-useful-businesses (sorry, just made the site live and I don’t want a link). Here’s a link to the full taxonomy template: https://www.bizharbour.net/projects/test-goto-archive.txt.

    I know this has been done before, perhaps a ksort or something but don’t know how to get this in my code.

    Cheers,
    Tracy

    Thread Starter Freelancealot

    (@freelancealot)

    Mystery solved. The brilliant and helpful Marcel Pol, author of Custom Taxonomy Order NE fixed the code for me.

    For anyone looking for an answer, here it is. First I installed Marcel’s plugin and then I did the following.

    Deleted this from the code in my post:

    $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
        foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
            $wpq = array (

    And inserted this in it’s place:

    $termchildren = get_terms( array(
        'taxonomy' => $taxonomyName,
        'child_of' => $current_term->term_id,
    ) );
    
        foreach ($termchildren as $term) {
            $wpq = array (

    Works like a charm.

    Cheers,
    Tracy

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Ordering Child Term Headings with list of posts in Custom Tax archive’ is closed to new replies.