How can I get the category for a specific blog?

Hi,
I have a custom query where I output all blogs from the $wpdb->blogs table, and I would like to output the category for each blog as a class name to the

  • I'm outputting blog link etc in.

    Any suggestions on how do I do that? :slight_smile:

    /olov

    • olov

      Hi Paul,
      Sorry, I was posting from the site category plugin page and just assumed that I was posting within that context .... My mistake.

      It's a WP multisite setup where I've installed the Site Categories plugin to be able to categorize the blog and then filter them. I'm going to have a jQuery script sort and filter out blogs for different blog categories depending on user input, so I need the blog category in the
      <li> element where I output the blogs.

      The basic setup I have now is that I grab all the blogs with

      $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = '1' AND deleted = '0' ORDER BY path ASC" ), ARRAY_A );

      and then output them with a foreach loop. My thought was that during that loop I should be able to get the blog category for each blog_id and assign that to my output.

      But maybe you have a better suggestion? :slight_smile:

    • Paul

      @olov, Excellent. I'm the author of the Site Categories plugin so surely I can provide some direction.

      There are help helper functions used as part of the Site Categories plugin code which provide the interface to:

      1. Site for a specific Site Category term -

      $site_categories->get_taxonomy_sites($term_id, $include_child = false);

      This will return an array of all sites for a specific term_id.

      2. wp_get_object_terms($current_site_id, SITE_CATEGORIES_TAXONOMY);

      This is of course a WordPress core function. This function given a site id 1, 32, 18, etc will return all Site Categories in an array for the given site.

      The important thing to remember is the Site Categories taxonomy is only available on the primary site within Multisite. So when calling these from a sub-site you need to first call

      global $current_site // Global WP var of the current site NOT the current_blog
      switch_to_blog($current_site->blog_id);

      // Do your query and stuff

      // The rest the blog to the real one.
      restore_current_blog();

      Hope this helps. I'd be interested to see the finished product you are building.

    • olov

      Thanks Paul, that was exactly what I was looking for. Works like a charm. I ended up putting it together in this function that might help someone else out:

      function get_blog_cat_slug($the_blogid) {
      	  global $current_site; // Global WP var of the current site NOT the current_blog
      	  switch_to_blog($current_site->blog_id);
      	  $blog_cat = wp_get_object_terms($the_blogid, SITE_CATEGORIES_TAXONOMY);
      	  restore_current_blog();
      
      	  if(!empty($blog_cat)){
      		if(!is_wp_error( $blog_cat )){
      		  foreach($blog_cat as $the_blog_cat){
      			$cat_slug .= $the_blog_cat->slug.' ';
      		  }
      		}
      	  }
      	  return $cat_slug;
      }

    Thank NAME, for their help.

    Let NAME know exactly why they deserved these points.

    Gift a custom amount of points.