0

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:

  1. It shows categories/tags that the current blog has no posts for, linking to an archive with a 'not found' message.
  2. 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?

2 Answers 2

0

The wp_dropdown_category seems to not accept the mera_query parameter codex Maybe you must perform à special query , note that the meta_query must be an array of array.

 'meta_query'=> array(
       array(
         'key'     => 'blog_id',
        'value'   => $blog_id,
        'compare' => '='
       )
 ),

With the new query you'll be able to compose your dropdown by looping through result.

WP_Query custom field parameter

Hope it works with this ?

3
  • Thanks, but no. Adding the extra array wiped out all the categories from the list. (Before adding the extra array, it displayed all categories with total multisite post count, which was same behavior as before adding the meta_query.)
    – jaboi
    Nov 19, 2016 at 18:25
  • I edit my answer, didn't notice the use ofwp_dropdown_category
    – Benoti
    Nov 19, 2016 at 18:39
  • Thanks. Good catch on pointing out meta_query not accepted by wp_dropdown_category. I'm trying to loop through the categories a different way, but no luck yet.
    – jaboi
    Nov 19, 2016 at 18:48
0

I know there has to be a better way, but this is what I could come up with. I'm not referencing blog_id at all, but I have to loop through all the posts on the current blog, category by category:

<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">
    <select class="postform" name="categories-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
        <option value=""><?php echo attribute_escape(__('Select Category')); ?></option>
            <?php
            $categories = get_categories( array(
                'orderby' => 'name',
                'order'   => 'ASC'
            ) );
            foreach( $categories as $category ) {
                $args = array(
                    'posts_per_page' => -1,
                    'category' => $category->term_id);
                $posts = get_posts($args);
                $ct = 0;
                foreach ($posts as $post) {
                    $ct++;
                }
                $c_list = '<option value="'.get_category_link($category->term_id).'">';
                $c_list .= $category->name.' ('.$ct.')';
                $c_list .= '</option>';

                if($ct > 0) { echo $c_list; }
          } 
          ?>
    </select>
</form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.