Navigation überspringen

[Gelöst] Question about sorting a taxonomy

This support ticket is created vor 6 days, 17 hours. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Zeitzone des Unterstützers: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 2 Antworten, hat 1 Stimme.

Zuletzt aktualisiert von shannonM-4 vor 1 day, 15 hours.

Assistiert von: Minesh.

Author
Artikel
#2862553
screenshot 2026.05.27 at 10.02.04@2x.png

Hi there,

I had a question about sorting a taxonomy. We have a two-level taxonomy named Services. I'd like to sort them by the number of items in the second level. (Not the number of posts, but the number of subcategories.) Please see attached for what we're trying to accomplish.

As you can see, the first column has 12 subcategories, the second column has 6 subcategories, and the third column has 4 subcategories. Is this possible to do? If so, how?

Thanks.

Saul

#2862649

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no such native way to do it but the possible way could be you need to add a custom term field and store the number of subcategories value for the parent term and then try to sort it accordingly. You can save the number of child terms to custom term field when you save the parent term.

If you do not know how to do it - please send me problem URL and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2863163

Minesh
Unterstützer

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

I've created the following term custom field group and added the term field "child-term-count" to it:
=> versteckter Link

Now, when you add/edit parent term, you can see the field. For example:
- versteckter Link

I've added a "Code Snippets" plugin and added a new code snippt and added the following code to it:
=> versteckter Link

add_action( 'created_service', 'sync_service_child_count', 10, 2 );
add_action( 'edited_service', 'sync_service_child_count', 10, 2 );

function sync_service_child_count( $term_id, $tt_id ) {

    $term = get_term( $term_id, 'service' );

    if ( is_wp_error( $term ) ) {
        return;
    }

    // Update the parent term
    $parent_id = $term->parent ?: $term_id;

    $children = get_terms( [
        'taxonomy'   => 'service',
        'parent'     => $parent_id,
        'hide_empty' => false,
        'fields'     => 'ids',
    ] );

    update_term_meta(
        $parent_id,
        'wpcf-child-term-count',
        count( $children )
    );
}

The above code will be used to set the number of total child terms to term field "child-term-count" we created using Toolset.

Now, can you please tell me where and on what page you want to display the terms based on the total child count - can you please share link of that page?

#2863205

Minesh,

Thanks for furnishing the code and explaining how it works. It's all pretty clear to me. I think the last step requires me to sort the view by the child-term-count, and I can do that.

No need for you to add it to the page, I can take it from here.

Thanks again!

Saul