Skip Navigation

[Résolu] Customer taxonomy filter ordering is breaking other filters

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I would like to define a custom order for the taxonomy terms in a custom search filter.

Solution: The options you have for taxonomy term order are somewhat limited, and do not correspond to the order you define in the display_values attribute. My recommendation is to use the following filter shortcode:

[wpv-control-post-taxonomy taxonomy="military-action" type="select" default_label="Select Conflict Below" url_param="wpv-military-action" orderby="id"]

Then add this custom filter to your child theme's functions.php file:

function custom_taxonomy_filter_order( $terms, $taxonomies, $args, $term_query ){
 
  global $required_orderby;
 
  // Specify taxonomy, current setting for orderby, and required order (slugs) here
  $taxonomy = "military-action";
  $current_orderby = "id";
  $required_orderby = array(
    "wwi","wwii","korean-war","vietnam-war","gulf-war","war-in-iraq","war-on-terror","unknown"
  );
 
  if ( $taxonomies[0] == $taxonomy && $args['orderby'] == $current_orderby ){
 
    usort( $terms, "custom_term_compare" );
 
  };
 
  return $terms;
}
add_filter( 'get_terms', 'custom_taxonomy_filter_order', 10, 4);
 
/**
 * Customise sort function
 *
 */
function custom_term_compare( $a, $b ){
 
  global $required_orderby;
 
  $a_key = array_search( $a->slug, $required_orderby );
  $b_key = array_search( $b->slug, $required_orderby );
 
  if ( $a_key == $b_key ) {
    return 0;
  }
  return ( $a_key < $b_key ) ? -1 : 1;
}

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_terms/

This support ticket is created Il y a 6 années. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Marqué : 

This topic contains 4 réponses, has 2 voix.

Last updated by Kristin Il y a 6 années.

Assisted by: Christian Cox.

Auteur
Publications
#629365
download.png

I have several taxonomy filters setup on my view, and I attempted to set a custom order on one of the filters. The filtering works as expected there, but my other taxonomy filters now return nothing. The filter I tried to customize is the one with the url parameter "wpv-military-action". Here is my code:

<div class="form-group">
[wpv-control-post-taxonomy taxonomy="military-branch" type="select" default_label="Select Branch Below" url_param="wpv-military-branch"]
</div>
<div class="form-group">
[wpv-control-post-taxonomy taxonomy="phs-graduation-year" type="select" default_label="Select Class Below" url_param="wpv-phs-graduation-year"]
</div>
<div class="form-group">
[wpv-control display_values="Select Conflict Below,WWI,WWII,Korean War,Vietnam War,Gulf War,War In Iraq,War on Terror,Unknown" values="0,wwi,wwii,korean-war,vietnam-war,gulf-war,war-in-iraq,war-on-terror,unknown" type="select" default_label="Select Conflict Below" url_param="wpv-military-action"]
</div>
<div class="form-group">
[wpv-control-post-taxonomy taxonomy="medal" type="select" default_label="Select Medal Below" url_param="wpv-medal"]
</div>

#629367

Also, the font end view can be seen here: hidden link

#629371

I should also mention that I added a taxonomy shortcode parameter to the problem shortcode, and that fixes the filtering, but the taxonomy terms are now ordered by name instead of my custom order:

[wpv-control taxonomy="military-action" display_values="Select Conflict Below,WWI,WWII,Korean War,Vietnam War,Gulf War,War In Iraq,War on Terror,Unknown" values="0,wwi,wwii,korean-war,vietnam-war,gulf-war,war-in-iraq,war-on-terror,unknown" type="select" default_label="Select Conflict Below" url_param="wpv-military-action"]

#629403

Hi, the options you have for taxonomy term order are somewhat limited, and do not correspond to the order you define in the display_values attribute. My recommendation is to use the following filter shortcode:

[wpv-control-post-taxonomy taxonomy="military-action" type="select" default_label="Select Conflict Below" url_param="wpv-military-action" orderby="id"]

Then add this custom filter to your child theme's functions.php file:

function custom_taxonomy_filter_order( $terms, $taxonomies, $args, $term_query ){

  global $required_orderby;

  // Specify taxonomy, current setting for orderby, and required order (slugs) here
  $taxonomy = "military-action";
  $current_orderby = "id";
  $required_orderby = array(
    "wwi","wwii","korean-war","vietnam-war","gulf-war","war-in-iraq","war-on-terror","unknown"
  );

  if ( $taxonomies[0] == $taxonomy && $args['orderby'] == $current_orderby ){

    usort( $terms, "custom_term_compare" );

  };

  return $terms;
}
add_filter( 'get_terms', 'custom_taxonomy_filter_order', 10, 4);

/**
 * Customise sort function
 *
 */
function custom_term_compare( $a, $b ){

  global $required_orderby;

  $a_key = array_search( $a->slug, $required_orderby );
  $b_key = array_search( $b->slug, $required_orderby );

  if ( $a_key == $b_key ) {
    return 0;
  }
  return ( $a_key < $b_key ) ? -1 : 1;
}
#629414

Thanks, Christian. That did the trick!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.