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.
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)
This topic contains 4 réponses, has 2 voix.
Last updated by Kristin Il y a 6 années.
Assisted by: Christian Cox.