• Hi Guys.

    I’ve got a WordPress site where i’m using Beautiful custom taxonomy filters with a particular custom post type, These posts are also assigned with a particular meta value based on a ‘type’.

    By setting a session variable on each of these sub-pages that matches the meta value, I can filter through only the posts that contain that meta, Using the code below in functions.php

    function thisCategory( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
    return;
    
    if ( is_post_type_archive( 'knowledge-center' ) ) {
    $cat = $_SESSION['kc-sub'];
    $metaquery = array(
    array(
                                   'key' => 'type', // name of custom field
                                   'value' => $cat,
                                   'compare' => 'LIKE',
    
                                    ),
                                );
                if ( isset($_SESSION["kc-sub"]) ) {
                $query->set( 'meta_query',$metaquery);
                return;
                } else {
    
                }
            }
        }
        add_action( 'pre_get_posts', 'thisCategory', 1 );

    But the only issue i’m having now, is the Dropdowns for the plugin, their still looking throughout the whole CPT – So i’m getting post counts for all posts registered within the selected taxonomy/taxonomies.

    What i’m trying to achieve is to have the dropdowns query that taxonomy, within posts that have a meta value of my session, So we can get accurate post counts displayed.

    I’ve been looking at the filter “beautiful_filters_dropdown_categories” (https://wordpress.org/plugins/beautiful-taxonomy-filters/other_notes/) which can manipulate wp_dropdown_categories, but i’m having strange results of all dropdowns displaying the same values using this method.

    If anyone can shed any light on this or point me in the right direction, it’d be much appreciated!

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author Jonathandejong

    (@jonathandejong)

    Hi,

    I see what you’re trying to achieve. The plugin uses WordPress core function https://codex.wordpress.org/Function_Reference/wp_dropdown_categories as you already figured out.

    That function then fetches the count value of each $term which you can’t really change yourself.

    The only thing I can think of is if you disable the post count and then use the filter beautiful_filters_term_name to modify the name with your own post count .. It has the term object as an extra parameter so you could run your own query with the term in the tax_query parameter along with your session variable. However I would not recommend this approach if you have a lot of terms since you’ll be running an extra query for each term.. Perhaps consider putting the values in some transients to solve the performance issue.

Viewing 1 replies (of 1 total)
  • The topic ‘dropdown post counts with pre_get_posts’ is closed to new replies.