Change term order of Taxonomy Frontend outpus

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Change term order of Taxonomy Frontend outpus

This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 10 months, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #35736
    willfaulds59
    willfaulds59
    Participant

    In the Frontend Search Settings options you can set Default term order to Name, Item Count or ID and Ascending or Descending.

    Can I gain more control using PHP and a filter such as asp_pre_get_front_filters or asp_pre_parse_filters.

    I’ve been digging around unsuccessfully for a few hours and can’t work out a method to do it. I would like to arrange some front filters in pairs and then add javascript to make them respond like toggles…

    #35741
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi!

    There is one hook, which may help you with that:

    $terms = apply_filters('asp_fontend_get_taxonomy_terms',
    	$terms,
    	$taxonomy,
    	array(
    		'orderby' => $term_ordering[0],
    		'order' => $term_ordering[1],
    		'include' => $terms,
    		'include_ids' => $term_ids
    	),
    	$all_terms_were_requested
    );

    The $terms is the array of taxonomy term objects returned via the get_terms(..) core wordpress function.
    You can either change the existing $terms array, or construct your own, and the plugin will display them in that order.

    Before output, the plugin will do a minor change after this hook – it may move child terms after their corresponding parent term for correct displaying. If the terms are alrady ordered correclty (parent -> child -> grandchild -> etc..), it should not affect the final output.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #35753
    willfaulds59
    willfaulds59
    Participant

    Excellent! Thank you.

    Posting to help others in the future

    add_filter( 'asp_fontend_get_taxonomy_terms', 'asp_reorder_terms', 1, 3 );
    function asp_reorder_terms($terms, $taxonomy, $args) {
    	if ( $taxonomy == 'product_tag' ) {//'product_tag' is for WooCommerce
    
    		$custom_order = [ 'slug2' , 'slug3' , 'slug4' , 'slug1' ];//desired order of terms using their slug
    		$terms_reordered = [];
    		$i = 0;
    		foreach($custom_order as $slug => $data){
    			foreach($terms as $term){
    				if($term->slug == $slug){
    					$terms_reordered[$i] = $term;
    					unset($term);//unset so mark it reordered
    				}
    			}
    			$i++;
    		}
    		return array_merge($terms_reordered, $terms);//merge in any terms not reordered
    	}
    
    	return $terms;
    }
    • This reply was modified 10 months, 1 week ago by willfaulds59 willfaulds59.
    #35760
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #35761
    willfaulds59
    willfaulds59
    Participant

    Thanks Ernest,

    One last addon before it closes…

    Some reasonably robust (perhaps you can suggest a better solution than using the ‘click’ event) and commented javascript for making filters act like toggles (in this case ONLY one can be one at a time, but both can be off, so NOT a true toggle).

    example of ASP toggles using javascript

    jQuery(window).ready(function($) {
    	function linkASPFrontendFilterEvent( $master, $slave ){
    		$master.on('click', function () {//must be ASP js that prevents 'change' working (even if override the input being hidden)
    			if( jQuery(this).find('input[type=checkbox]').is(':checked') ) {//must be ASP js that prevents .checked working
    				$slave.find('input[type=checkbox]').prop( "checked", false );
    			}
    		});
    	}
    
    	function linkASPFrontendFilter( term1, term2 ){
    		if( jQuery( '.'+term1) !== null && jQuery( '.'+term2) !== null ){
    			var $elem1 = jQuery( '.asp_option.'+term1 );
    			var $elem2 = jQuery( '.asp_option.'+term2 );
    			linkASPFrontendFilterEvent( $elem1, $elem2 );
    			linkASPFrontendFilterEvent( $elem2, $elem1 );
    		};
    	};
    
    	//array toggle classes to be linked (Requires a theme override e.g. asp-tax-checkboxes.php with "str_replace(' ', '-', strtolower(strip_tags($term->label))); ?> ")
    	const ao_asp_toggles = [
    		['alcoholic','non-alcoholic'],
    		['sparkling','still'],
    	];
    	//loop through the classes
    	for (i = 0; i < ao_asp_toggles.length; ++i) {
    		linkASPFrontendFilter( ao_asp_toggles[i][0], ao_asp_toggles[i][1] );
    	};
    });
    • This reply was modified 10 months, 1 week ago by willfaulds59 willfaulds59.
    • This reply was modified 10 months, 1 week ago by willfaulds59 willfaulds59.
    • This reply was modified 10 months, 1 week ago by willfaulds59 willfaulds59.
    • This reply was modified 10 months, 1 week ago by willfaulds59 willfaulds59.
    #35777
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    This looks okay to me. It should support any future updates, and that is the most important point.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.