Skip Navigation

[Resolved] sorting taxonomy terms in a content template

This support ticket is created 3 years, 9 months ago. 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by LennyS7960 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2138003

I want to display a post taxonomy in a post template, but there only are 2 options to sort - Asc and Desc. But I need to sort them so the the primary taxonomy would be 1st, then the other taxonomy.

#2139219

Hello there, I would like to follow up with you on yesterday's chat issue. Does the solution that I suggested worked for you? Do you have any further questions?

#2139361

Hi,
It didn`t work.
The first code provided in that solution did work, but I had the same problem as that guy that I needed the name and not the link.
And the last code in the solution:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy']; // change this to your taxonomy
    $separator = '';
    $out = '';
    $term_ids = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    usort( $term_ids, 'cmp' );
    foreach($term_ids as $term_id ) {
      $term = get_term( $term_id, $taxonomy, OBJECT );
      $out .= $separator . $term->name;
      $separator = $atts['separator'];
    }
    return $out;
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

didn`t sort it hierarchical order.

#2141999

The first code produces links to the taxonomy terms archives. You don't want links, you just need the terms, right?
In that case, I updated the code to use the strip_tags functions that will remove any HTML tags from the results. The code becomes:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy'];
    $separator = $atts['separator'];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        return strip_tags( wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none") );
          
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

And you can use it like this:

  [display_post_tax_terms_hierarchical taxonomy="category" separator=" - "]

I tested it on my local setup with the regular posts and categories. Please test it from your side, and if it does not work, let me know what custom post type and custom taxonomy you are working on. Let me know if it would be ok for you to allow me temporary access to your website and let me check it further.

#2142177

Thanks, this code works, but there still is a small problem - after the last category there is an extra seperator.
Instead of "First category - Second category" I get "First category - Second category -"
If it is needed I can give you access to my site

#2142523

Please try this code:

function list_hierarchical_terms($atts) {
    global $post;
    $taxonomy = $atts['taxonomy'];
    $separator = $atts['separator'];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
		$results = wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
		$results = rtrim( $results );
		$results = rtrim( $results, $separator );
		return strip_tags( $results );
          
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

If it does not work, I'll need to take a closer look at your website. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please let me know where do you show this.

#2142545

My issue is resolved now. Huge thank you! By the way, maybe it could be added as a feature request, because I had the same problem with a view page where I wanted to sort hierarhicaly, but needed custom php. Atleast I think it makes a lot of sense to sort it like this and not Asc or Desc.