Marketpress Tag Cloud shows tags from unpublished posts

The Marketpress tag cloud is showing tags attached to unpublished posts (on my Breastfeeding Clothes page, the current tag cloud shows 91 topics for the tag "BCBGeneration" but all the attached products are currently unpublished, leading to a rather frustrating customer experience. Is there any way of customising the tag cloud so it only calls tags attached to published posts?

  • sequin
    • Site Builder, Child of Zeus

    HI Michael,
    Access enabled. At the moment it's using the standard wp_tag_cloud() function but I had been using the product tag cloud widget from Marketpress. I was hoping it was a Marketpress glitch but looks like standard functionality for custom posts in Wordpress.

    I'd be happy to use a query to sort it, but can't for the life of me figure out what that query would be!

  • Michael Bissett
    • Recruit

    Hey @sequin! :slight_smile:

    After doing a bit of digging into this, you may wish to try the code snippet shared here (inserting it into your child theme's functions.php):

    http://jeffri.me/2010/08/filter-get_terms-to-return-only-terms-with-published-post/

    Since the wp_tag_cloud function utlizes the get_terms function, this should take care of those tags with unpublished posts. :slight_smile:

    Let me know if this helps please!

    Kind Regards,
    Michael

  • sequin
    • Site Builder, Child of Zeus

    Brilliant thanks! Having played around with the config a little using the code from the comments, the code I finally used to fix the issue was:

    function get_terms_filter( $terms, $taxonomies, $args )
    {
    	global $wpdb;
    	$taxonomy = $taxonomies[0];
    	if ( ! is_array($terms) && count($terms) < 1 )
    		return $terms;
    	$filtered_terms = array();
    	foreach ( $terms as $term )
    	{
    $result = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts p, $wpdb->term_relationships rl, $wpdb->term_taxonomy wpt WHERE p.ID = rl.object_id AND rl.term_taxonomy_id = wpt.term_taxonomy_id AND wpt.term_id = $term->term_id AND p.post_status = 'publish' LIMIT 1");
    		if ( intval($result) > 0 )
    			$filtered_terms[] = $term;
    	}
    	return $filtered_terms;
    }
    if( !is_admin() ){add_filter('get_terms', 'get_terms_filter', 10, 3);}

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.