Skip Navigation

[Resolved] Ordering a taxonomy filter by a custom term field

This support ticket is created 3 years, 11 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
- 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 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by Minesh 3 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1921413

I have a View that filters by a Taxonomy and I have created a custom term field "category-order" for that taxonomy. I'd like to order this taxonomy on the front-end filter, but it seems there's no option to order by a custom term field?

hidden link

Thanks,

Tim

#1921563

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

As I understand, you have added the custom term field to your taxonomy and you are displaying this taxonomy as frontend filter with the view.

But you want to display the terms with the taxonomy frontend filter in the specific order you added to term custom field. If this is correct - Can you please share problem URL where you added your view as well as 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.

#1921713

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link


function func_order_terms_by_term_custom_field( $terms, $taxonomies, $args, $term_query ){
  $pages = array(2059 ); // comma-separated list of page IDs where this Form is displayed
  $taxonomy_slug = 'video-target-grade'; // slug of the taxonomy to filter
  $order_field_slug = 'category-order'; // slug of the term order custom field
 
  
  $ordered_terms = array();
  if ( is_page( $pages ) && $taxonomies[0] == $taxonomy_slug ) {
      foreach($terms as $key=>$term)  {
        $term_order = get_term_meta($term->term_id, 'wpcf-' . $order_field_slug, true);
        $ordered_terms[$term_order] = $term; 
      }
      ksort( $ordered_terms, SORT_NUMERIC ); 	
      $terms = $ordered_terms;
    }
  return $terms;
}
add_filter( 'get_terms', 'func_order_terms_by_term_custom_field',10,4);

Can you pleases confirm it works as expected and terms are ordered by custom term field.

#1922571

Thank. So it seems if I have the View set to "Show only available options for each input" then it works in the beginning but once a filter is selected then it returns to the default order. But if I have it set to "Always show all values for inputs" then it works. I guess I can keep it set to this but is there no way to have it work in both situations?

Tim

#1922717

Minesh
Supporter

Languages: English (English )

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

Yes - you will to go with the option "Always show all values for inputs" because there is no alternative or workaround available.

#1924669

Ok, I can use that option. But now I see one other issue, when that code is activated the Target Grade and Instrument Families shortcodes within the view (not the filter) suddenly disappear:

hidden link

Until you actually click on a filter, then they appear.

Tim

#1927375

Minesh
Supporter

Languages: English (English )

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

The code was broken because I see you removed the category order field value for your every term.

You must add category-order custom term field value for your every term:
=> hidden link

Can you please do that and let me know if that helps.

#1930819

Actually I just decided to delete and recreate the categories in the order I need, this solution was not quite working. But in the future are their plans to allow ordering by taxonomy custom term? That would be quite helpful.

#1930953

Minesh
Supporter

Languages: English (English )

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

I'll discuss the feature to sort term by custom term field for the taxonomy filter.

As you know, currently there is no other option/feature available. Either you have to go with the code I shared or drop the idea to sort terms by custom term field.