Title: dropdown post counts with pre_get_posts
Last modified: August 24, 2016

---

# dropdown post counts with pre_get_posts

 *  [fishr](https://wordpress.org/support/users/fishr/)
 * (@fishr)
 * [11 years ago](https://wordpress.org/support/topic/dropdown-post-counts-with-pre_get_posts/)
 * 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/](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](https://wordpress.org/support/users/jonathandejong/)
 * (@jonathandejong)
 * [11 years ago](https://wordpress.org/support/topic/dropdown-post-counts-with-pre_get_posts/#post-6127201)
 * Hi,
 * I see what you’re trying to achieve. The plugin uses WordPress core function 
   [https://codex.wordpress.org/Function_Reference/wp_dropdown_categories](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.

 * ![](https://ps.w.org/beautiful-taxonomy-filters/assets/icon-256x256.png?rev=1654967)
 * [Beautiful taxonomy filters](https://wordpress.org/plugins/beautiful-taxonomy-filters/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/beautiful-taxonomy-filters/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/beautiful-taxonomy-filters/)
 * [Active Topics](https://wordpress.org/support/plugin/beautiful-taxonomy-filters/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/beautiful-taxonomy-filters/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/beautiful-taxonomy-filters/reviews/)

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [dropdowns](https://wordpress.org/support/topic-tag/dropdowns/)
 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)

 * 1 reply
 * 2 participants
 * Last reply from: [Jonathandejong](https://wordpress.org/support/users/jonathandejong/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/dropdown-post-counts-with-pre_get_posts/#post-6127201)
 * Status: not resolved