• Resolved destinedjagold

    (@destinedjagold)


    Hello and good day everyone.
    I wrote a custom tag cloud for my WordPress multisite with the help of code references available online.
    Let me show you guys the basic structure of my code…

    <?php
    $arr_tags = array();
    $rows = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id from $wpdb->blogs WHERE public = %s AND archived = %s AND mature = %s AND spam = %s AND deleted = %s",'1','0','0','0','0' ) );
    
    if(!empty($rows)){
    	$tags_count = 0;
    
    	foreach ( $rows as $row ) {
    		switch_to_blog($row->blog_id);
    		$arrgs = new WP_query(array(
    			'orderby' => 'date',
    			'order' => 'DESC',
    			'post_type' => 'post'
    		));
    
    		while($arrgs->have_posts()){
    			$arrgs->the_post();
    			$posttags = get_the_tags(get_the_ID());
    
    			if ($posttags) {
    				foreach($posttags as $tag) {
    					$tag_list[$tags_count++] = array($tag->name, $tag->count);
    					$tags_count++;
    				}
    			}
    		}
    	}
    
    	echo '<div class="tag_cloud">';
    
    		$tag_posts_temp="";
    
    		$newArr = array();
    		foreach ($tag_list as $val) {
    				$newArr[$val[0]] = $val;
    		}
    		$tag_list2 = array_values($newArr);
    
    		for($x=0;$x<count($tag_list2);$x++){
    			$y=0;
    			if($tag_list2[$x][$y] != $tag_posts_temp){
    				echo $tag_list2[$x][$y] . '(' . $tag_list2[$x][$y+1] . ') ';
    			}
    			$tag_posts_temp = $tag_list2[$x][$y];
    		}
    
    	echo '</div>';
    }
    ?>

    The code works but I’m having problems with the possibility of having identical tags from the different subsites. :/ Basically, I want the duplicate tags to be unified, while also their values being added. So if there are two identical [monitor] tags but have different post counts; [2] and [5], I want them to be unified into [monitor][7].

    Can anyone tell me how I can solve this little problem of mine? Thanks~

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    πŸ³οΈβ€πŸŒˆ Advisor and Activist

    You’ll have to do either some massive switch_to_blog shenanigans OR direct SQL table merge queries.

    Either way, you’ve run into something that Multisite actually isn’t good at.

    Multiple separate sites. I have to stress this. You’re in your own brave new world πŸ™

    Thread Starter destinedjagold

    (@destinedjagold)

    I see… Well, is there a multisite plugin that will unify all tags? I’ve found [Multisite Global Terms], however it’s not really compatible with WPMu v4. (Found that out the hard way. Now I’ll have to readd all categories and tags again…)

    Thread Starter destinedjagold

    (@destinedjagold)

    I use https://wordpress.org/plugins/wordpress-mu-sitewide-tags/ to do that.

    This plugin seems promising. Thanks for sharing! I’ll try it out now. ^^

    Thread Starter destinedjagold

    (@destinedjagold)

    Okay, so I find that plug-in too confusing for me. The plugin has little to no instructions of how to use it. How can I search posts with a certain tag with this plugin? How am I going to create a cloud tag with this plugin? :/

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    πŸ³οΈβ€πŸŒˆ Advisor and Activist

    The plugin copies posts from your network to one site. Then on that site you’d have a tag cloud, same as anywhere else, and use that.

    Thread Starter destinedjagold

    (@destinedjagold)

    Ah! I understand it now. Thanks.

    So, after I enabled the plugin, it created a new site called [Global Post]. Now I have no clue what to do next. I’ve tried checking the [All blogs] option in the [Populate Posts] section, yet the [Global Post] page is showing no changes whatsoever… The [Global Post] page is only showing me that [Hello World!] blog post. :/

    Anyone can tell me what should I be doing next?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    πŸ³οΈβ€πŸŒˆ Advisor and Activist

    You have to post.

    Read https://wordpress.org/plugins/wordpress-mu-sitewide-tags/other_notes/

    “”Populate Posts” allows you to fill in posts from an existing blog.”

    Thread Starter destinedjagold

    (@destinedjagold)

    I believe I understand how it works now. So basically, after activating the plug-in, whenever I create a new post, it’ll also be posted in the [Global Post] blog. And then, I can use that blog to create a tag cloud.

    Sad that it doesn’t copy the already-published posts though… But oh well.

    Many thanks!

    Thread Starter destinedjagold

    (@destinedjagold)

    Okay, so it’s working wonderfully now. I turned [Global Post] page into a custom tag cloud. ^^

    But now I have a question… How can I paste that whole page into another page’s sidebar?

    Thread Starter destinedjagold

    (@destinedjagold)

    Hm… So I tried calling the page with this code for testing purposes…

    $pages = get_pages(array('include' => '2'));
    foreach($pages as $tsk){
    	$content = $tsk->post_content;
    
    	echo $tsk->ID; // to see if I can retrieve...anything from the Global Post page...
    }

    It works, I suppose, however, I can’t get the “page content.” My [Global Post] page uses a custom template wherein it only displays a list of tags now, like a normal tag cloud. I couldn’t figure out how to display the [Global Post]’s contents to the page where I want the tag cloud to be displayed. :/

    Thread Starter destinedjagold

    (@destinedjagold)

    Once again, I have fixed my own problems. ^^

    I would like to thank [David Sader] for letting me know the existence of [WordPress MU Sitewide Tags Pages] plugin, and [Ipstenu (Mika Epstein)] for being patient and helpful. ^^

    Anyway, with the help of the [MU Sitewide Tags] plugin, I was able to create my custom tag cloud that I needed for WordPress MultiSite. ^^

    This is the code that I used. I’m just going to post it here for future reference.

    <?php
    	echo '<div class="widget tag_cloud">';
    		include(locate_template('tag_cloud.php'));
    	echo '</div>';
    ?>

    That’s the code I added in my sidebar. And then, here’s the tag_cloud.php code. I removed the fancy CSS styling in the code, by the way.

    <?php
    /**
     * Template Name: tag_cloud_template_(global_post)
     *
     * created by Joseph B.
     */
    
    	$tagcloud = 16;
    	switch_to_blog($tagcloud);
    
    	$args = array(
    		'post_type'   => 'post',
    		'orderby'    => 'rand',
    		'order'    =>  ''
    	);
    
    	$myposts = get_posts( $args );
    
    /* create tag list */
    	foreach ( $myposts as $post ){
    		$categories = get_the_category($post->ID);
    		$posttags = get_the_tags(get_the_ID());
    
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$list_of_tags[] = array($tag->name, $tag->count);
    			}
    		}
    	}
    /* end of create tag list */
    /* ---------------------- */
    
    /* remove identical entries inside the array */
    /* although this code is now using the
     * [MU Sitewide Tags] plugin, I don't think
     * that checking and removing identical entries
     * are needed... :/ */
    	$newArr = array();
    
    	foreach ($list_of_tags as $val) {
    		$newArr[$val[0]] = $val;
    	}
    	$tag_list = array_values($newArr);
    /* end of remove identical entries inside the array */
    /* ------------------------------------------------ */
    
    /* display results */
    	for($x=0; $x<count($tag_list); $x++) {
    		$tag_name = $tag_list[$x][0];
    		$tag_post_count = $tag_list[$x][1];
    
    		echo '<a href="' . network_home_url() .'tag?tag=' . $tag_name . '" class="tag_cloud_link">';
    
    		echo '<span>' . $tag_name . '(' . $tag_post_count . ') </span>';
    
    		echo '</a>';
    	}
    /* end of display results */
    /* ---------------------- */
    
    	restore_current_blog();
    ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom Tag Cloud – Problems with duplicate tags’ is closed to new replies.