Skip Navigation

[Gelöst] Customer taxonomy filter ordering is breaking other filters

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 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.

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by Kristin vor 3 Jahre, 6 Monate.

Assigned support staff: Christian Cox.

Author
Artikel
#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!