• Great Plugin! Especially the new Ajax feature to exclude empty-results filter combinations is very promising.

    However I do have some issues with it:

    If I select “show post count of terms” in the advanced options, the displayed numbers at the front-end are absolute and don’t take the other filter’s setting in account. (Ajax on or not).
    Is it possible to automatically update the post-count for the terms, so that the number represents the amount of posts that the user can actually expect in the query? (Considering the other filters that are set)

    On my website I want users to select a term from my main taxonomy “angebotscat = arbeit” by clicking on the navigation and then be able to filter down through secondary taxonomies (stadteil = arsten). So the user is always guided into a post-type-archive with the main taxonomy term preselected.
    Let me show you an example from my archive-main_taxonomy.php post type archive template:
    Post Type Archive Output
    This is the url you’re looking at: (sorry for the use of non-english terms)
    wtb = local directory of this wordpress instance
    angebotscat/arbeit = my main taxonomy / term
    stadteil/arsten = one of 3 secondary taxonomies / term

    Normally I’ve hidden (in CSS) the first filter field with the main taxonomy, to keep the user in the same main taxonomy term, but this brings up an issue: As you can see, the secondary taxonomy term “Arsten” has a higher post count (61) than the main taxonomy term “Arbeit” (33). If the user can’t see the filter dropdown, they will very likely be confused when the numbers don’t correspond to the post-query output.

    So I thought what if I can at least count only posts that are both in my main taxonomy “angebotscat” + “current term” as well as in each secondary taxonomy term.

    Looking at your code I tried a dirty little hack at public/class-beautiful-taxonomy-filters-public.php, but that returned 0 for all other taxonomies in the filters.

    See my faulty hack in the tax_query here:

    	/**
    	 * Fetch post count for terms based on a single post type
    	 *
    	 * @since	1.2.8
    	 */
    	public static function get_term_post_count_by_type($term, $taxonomy, $post_type){
    
    	    $args = array(
    	        'fields' =>'ids',
    	        'update_post_meta_cache' => false,
    	        'no_found_rows' => true,
    	        'posts_per_page' => 10000, // We don't set this to -1 because we don't want to crash ppls sites which have A LOT of posts
    	        'post_type' => $post_type,
    	        'tax_query' => array(
    				'relation' => 'AND',
    				array(
    					'taxonomy' => $taxonomy,
    	                                'field' => 'slug',
    	                                'terms' => $term
    				),
    				array(
    					'taxonomy' => 'angebotscat', //my pre-selected main taxonomy
    					'field' => 'slug',
    	                                'terms' => $term
    				),
    			),
    	     );
    	    $postcount_query = new WP_Query( $args );
    
    	    return ( count($postcount_query->posts > 0) ? count($postcount_query->posts) : 0 );
    
    	}
    

    What am I doing wrong here?

    Also will there possibly be a future admin option to properly include taxonomies in the rewrite but exclude from filter dropdown display?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jhtjards

    (@jhtjards)

    I somehow managed to get my hack to work, but I don’t really understand why ??
    cur_taxonomy should return any selected taxonomy, right? It only returns ‘angebotscat’ in my case. Which is great for me but feels to me like there could be issues further down the road.

    Here’s the updated code snippet:

    	/**
    	 * Fetch post count for terms based on a single post type
    	 *
    	 * @since	1.2.8
    	 */
    	public static function get_term_post_count_by_type($term, $taxonomy, $post_type){
    
    	    $queried_object = get_queried_object();
    	    //print_r($queried_object); 
    	    $cur_taxonomy = $queried_object->taxonomy;
    	    $cur_term_id = $queried_object->term_id; 
    	    $cur_post_id = $cur_taxonomy.'_'.$term_id;
    
    	    $args = array(
    	        'fields' =>'ids',
    	        'update_post_meta_cache' => false,
    	        'no_found_rows' => true,
    	        'posts_per_page' => 10000, // We don't set this to -1 because we don't want to crash ppls sites which have A LOT of posts
    	        'post_type' => $post_type,
    	        'tax_query' => array(
    	        	'relation' => 'AND',
    				array(
    					'taxonomy' => $taxonomy,
    	                                'field' => 'slug',
    	                                'terms' => $term
    				),
    				array(
    					'taxonomy' => $cur_taxonomy,
    					'field' => 'term_id',
    	                                'terms' => $cur_term_id
    				),
    			),
    	     );
    	    $postcount_query = new WP_Query( $args );
    	    return ( count($postcount_query->posts > 0) ? count($postcount_query->posts) : 0 );
    
    	}
    Plugin Author Jonathandejong

    (@jonathandejong)

    Hi,

    Thank you for those kind words. Yeah the feature is still in beta because I figured things like this would come up over time ??

    I’ve looked into updating the term count along with the conditional selects (AJAX). It’s not as easy as I’d hope but I will try to make it work.
    Hopefully it’d be fixed in a future update soon enough.

    Thread Starter jhtjards

    (@jhtjards)

    Hi Jonathan,
    great to hear! I’m looking forward to it!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Relative post count in filter terms’ is closed to new replies.