I am using this function to make categories and tags global on my multisite installation.
An unfortunate side effect of global categories/tags is that it displays categories for posts across all sites. This creates two undesirable problems:
- It shows categories/tags that the current blog has no posts for, linking to an archive with a 'not found' message.
- For count, WordPress looks at all of the blogs in the network, finds the highest total for each category/tag from all blogs within the network and displays that number.
When I display categories or tags in a dropdown on an individual site, I only want categories from the current blog_id to show in the list with the count from the number of corresponding posts in the current blog.
I have tried to limit the posts in a category dropdown with meta_query, but it's not working for me:
<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
$blog_id = get_current_blog_id();
$args = array(
'show_option_none' => __( 'Select Category' ),
'show_count' => 1,
'orderby' => 'name',
'echo' => 0,
'meta_query' => array(
'key' => 'blog_id',
'value' => $blog_id,
'compare' => '=')
);
$select = wp_dropdown_categories( $args );
$replace = "<select$1 onchange='return this.form.submit()'>";
$select = preg_replace( '#<select([^>]*)>#', $replace, $select );
<noscript>
<input type="submit" value="View" />
</noscript>
</form>
Any way to modify this meta_query parameter or tackle this from another angle?