The MU forums have moved to WordPress.org

Get categories of all blogs (3 posts)

  1. Akashic
    Member
    Posted 16 years ago #

    Hi,

    does anyone tried to pull out from DB all categories from all blogs?
    Everything else is quite simple, but because of taxonomy, I'm having hard times with categories.

    Best regards,
    A.

  2. Akashic
    Member
    Posted 16 years ago #

    Manage to think of something. Here's the idea:

    get all 'safe-to-show' blogs

    $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'");

    get all info on a post - along with categories

    foreach ($blogs as $blog){
    $post = $wpdb->get_results("SELECT * FROM wp_{$blog}_options
    LEFT JOIN wp_{$blog}_term_relationships ON
    (wp_{$blog}_posts.ID = wp_{$blog}_term_relationships.object_id)
    LEFT JOIN wp_{$blog}_term_taxonomy ON
    (wp_{$blog}_term_relationships.term_taxonomy_id = wp_{$blog}_term_taxonomy.term_taxonomy_id)
    LEFT JOIN wp_{$blog}_terms ON
    (wp_{$blog}_term_taxonomy.term_id = wp_{$blog}_terms.term_id)
    WHERE wp_{$blog}_posts.post_status = 'publish'
    AND wp_{$blog}_term_taxonomy.taxonomy = 'category'
    ORDER BY post_date DESC");
    }

    Now to put it in a nice loop. Should post when I'm done (err, any ideas? ;)
    Btw: you call for a 'category' by
    echo $post->name

  3. Akashic
    Member
    Posted 16 years ago #

    Ok, so for sure there was an easier way to do this (calling 'the_category()' function and so on instead of creating new 'post' query to the database, but I want to manipulate the data of every post in every blog to create dynamic sidemenu displaying the links to other articles/blogs according to the post content (sth like adsense but within the MU). Therefore I need maximum control and better handling of the code ;)

    Anyway, here's the code:

    $beforeBlock = "";
    $afterBlock = "<br><br>";       
    
    global $wpdb;
    $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    	public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
    	ORDER BY last_updated DESC");
    
    if ($blogs) {
    	foreach ($blogs as $blog) {
    		$options = $wpdb->get_results("SELECT option_value FROM
    			wp_{$blog}_options WHERE option_name IN ('siteurl','blogname')
    			ORDER BY option_name DESC");
    		$posts = $wpdb->get_results("SELECT * FROM wp_{$blog}_posts
    			LEFT JOIN wp_{$blog}_term_relationships ON
    			(wp_{$blog}_posts.ID = wp_{$blog}_term_relationships.object_id)
    			LEFT JOIN wp_{$blog}_term_taxonomy ON
    			(wp_{$blog}_term_relationships.term_taxonomy_id = wp_{$blog}_term_taxonomy.term_taxonomy_id)
    			LEFT JOIN wp_{$blog}_terms ON
    			(wp_{$blog}_term_taxonomy.term_id = wp_{$blog}_terms.term_id)
    			WHERE wp_{$blog}_posts.post_status = 'publish'
    			AND wp_{$blog}_term_taxonomy.taxonomy = 'category'
    			ORDER BY post_date DESC");	
    
    		if($post) {
    			foreach($posts as $post) {
    				//display only to show that it's working ;>
    				echo $beforeBlock.
    				'<a href="'.$post->guid.'">'.$post->post_title.'</a><br/>
    				by <a href="'.$options[0]->option_value.'">'.$options[1]->option_value.'</a>
    				in '.$post->name
    				.$afterBlock;
    			}
    		}
    	}
    }

About this Topic