Skip Navigation

[Resolved] How to control the display order of taxonomies

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to modify the sort orderby criteria for the terms returned by the wpv-post-taxonomy shortcode.

Solution: It's not currently possible to modify the shortcode this way, but I will recommend that as a future enhancement. In the meantime, the following code can be used:

add_shortcode( 'terms_ordered_by', 'terms_ordered_by_func');
function terms_ordered_by_func($atts) {
  $post_id = $atts['postid'];
  $tax = $atts['tax'];
  $i = 0;
  $return = '';
  $args = array('orderby' => 'slug', 'order' => 'ASC');
  $post_terms = wp_get_object_terms( $post_id, $tax, $args );
  if ( ! empty( $post_terms ) ) {
    if ( ! is_wp_error( $post_terms ) ) {
        foreach( $post_terms as $term ) {
         $return .= ($i ? ', ' : '') . '<a href="' . get_term_link( $term->slug, $tax ) . '">' . esc_html( $term->name ) . '</a>';
          $i++;
        }
    }
  }
  return $return;
}

Use the custom shortcode on your site as follows:

[terms_ordered_by postid="12345" tax="your-taxonomy-slug"]

Replace 12345 with the ID of a post, and replace your-taxonomy-slug with the slug of the taxonomy that contains the C# terms. If you would like to make the post ID dynamic depending on where you place the shortcode, then you can use the wpv-post-id shortcode instead of hard-coding the post ID:

[terms_ordered_by postid="[wpv-post-id]" tax="your-taxonomy-slug"]
This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 4 voices.

Last updated by brianD-10 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#583799

I have setup some custom taxonomies like:

C10
C9.x
C8.x
C7.x
C6.x
C5.5

While I can use a plugin to sort the order of these on the back end so that they appear in the correct order (as shown above) for adding a new custom post, when I go to display it in a layout, it is always displayed (if all fields are checked) as:

C10, C5.5, C6.x, C7.x, C8.x, C9.x

How do I get the front end to display them in the same order I have them on the back end?

I've tried custom slugs with two letters and a number, but the display always seems to be taken from the text value.

Thanks for the help!

#584585

The shortcode wpv-post-taxonomy implements alphabetical sorting. You can specify ascending or descending order, but not another sorting algorithm. When numbers are included in alphabetical sorting, you can get some unexpected results because C10 will rank lower than C5 in true alphabetical order. One simple way to address this is to use preceding zeros like C05 instead of C5. Otherwise, you're probably going to need a custom code solution that parses the text and strips out the leading character, then sorts the terms in numeric order, or sorts them by a custom field somehow.

The documentation for wp_get_object_terms can be found at the link below:
https://codex.wordpress.org/Function_Reference/wp_get_object_terms

#584693

Thanks for the information ... I guess.

Prepending a 0 to the numbers is simply not an option for me as that would change the meaning of the taxonomies to something irrelevant to the content.

It would seem that the best order of action would be if your wpv-post-taxonomy shortcode supported passing a sort order as an optional parameter.

Then developers could simply pass one of the valid arguments for the Codex function you referenced to it and the problem would be resolved. In my case I could easily use custom slug names that would sort in the expected order.

Unfortunately I have no idea how to implement what you suggested as a code solution (or what you referred me to in the codex) in the Layouts Editor.

#584917

Adding some flexibility to the shortcode is a great idea. I'll pass that along to our developers as a possible improvement. I can't guarantee they'll decide to implement it, so in the meantime I have a custom shortcode you can use to sort terms by slug and display a link to the term archive:

add_shortcode( 'terms_ordered_by', 'terms_ordered_by_func');
function terms_ordered_by_func($atts) {
  $post_id = $atts['postid'];
  $tax = $atts['tax'];
  $i = 0;
  $return = '';
  $args = array('orderby' => 'slug', 'order' => 'ASC');
  $post_terms = wp_get_object_terms( $post_id, $tax, $args );
  if ( ! empty( $post_terms ) ) {
    if ( ! is_wp_error( $post_terms ) ) {
        foreach( $post_terms as $term ) {
         $return .= ($i ? ', ' : '') . '<a href="' . get_term_link( $term->slug, $tax ) . '">' . esc_html( $term->name ) . '</a>';
          $i++;
        }
    }
  }
  return $return;
}

Use the custom shortcode on your site as follows:

[terms_ordered_by postid="12345" tax="your-taxonomy-slug"]

Replace 12345 with the ID of a post, and replace your-taxonomy-slug with the slug of the taxonomy that contains the C# terms. If you would like to make the post ID dynamic depending on where you place the shortcode, then you can use the wpv-post-id shortcode instead of hard-coding the post ID:

[terms_ordered_by postid="[wpv-post-id]" tax="your-taxonomy-slug"]
#585069

Thanks for the code!

I created a child theme and added the code to a functions.php file.

Then I called like this in the Toolset Layouts Editor:

Supported Versions: [terms_ordered_by postid="[wpv-post-id]" tax="supported-version"]

The code DOES display the taxonomies in the correct order (once I changed the slugs to be a sequence like sv1, sv2, sv3, etc).

BUT there is one problem.

It breaks the HTML output in the layout.

If I insert the regular shortcode for the taxonomy from the button I get this:

Supported Versions: [wpv-post-taxonomy type="clarion-version"]

That works correctly and the output comes after the words "Supported Versions".

But if I use the shortcode for the new function the output is generates into the upper left corner of the layout area instead of after the words "Supported Versions".

Any idea why or how to fix it?

Thanks!

#585162

y.S

Shortcodes are not supposed to output (echo) anything directly, they should return a value to the function.
Try this.

add_shortcode( 'terms_ordered_by', 'terms_ordered_by_func');
function terms_ordered_by_func($atts) {
  $post_id = $atts['postid'];
  $tax = $atts['tax'];
  $links = array();
  $args = array('orderby' => 'slug', 'order' => 'ASC');

  $post_terms = wp_get_object_terms( $post_id, $tax, $args );

  if ( ! empty( $post_terms ) ) {
     if ( ! is_wp_error( $post_terms ) ) {
         foreach( $post_terms as $term ) {
            $links[] =  '<a href="' . get_term_link( $term->slug, $tax ) . '">' . esc_html( $term->name ) . '</a>';
         }
     }
  }

 return implode(",", $links);
}
#585326

Thanks - that works perfectly!

I also appreciate the tip about shortcodes not outputting directly (which makes perfect sense).

#585331

Ah yes, error on my part. I've updated the code for future reference.

#628574

Christian's code was great but had conflicts with passing in a dynamic post_id. I resolved by resolving the post_id in the php.

add_shortcode( 'terms_ordered_by', 'impreza_child_terms_ordered_by_func');
function impreza_child_terms_ordered_by_func($atts) {
  // $post_id = $atts['postid'];
  $post_id = do_shortcode('[wpv-post-id]');
  $tax = $atts['tax'];
  $seperator = $atts['seperator'];
  $i = 0;
  $return = '';
  $args = array('orderby' => 'slug', 'order' => 'ASC');
  $post_terms = wp_get_object_terms( $post_id, $tax, $args );
  if ( ! empty( $post_terms ) ) {
    if ( ! is_wp_error( $post_terms ) ) {
        foreach( $post_terms as $term ) {
         $return .= ($i ? $seperator : '') . esc_html( $term->name );
          $i++;
        }
    }
  }
  return $return;
}

The forum ‘Types Community Support’ is closed to new topics and replies.