Forums

How to limit tags list to only the ones with the top 10 most posts(custom query) (1 post)

  1. XLCowBoy
    Member
    Posted 1 year ago #

    Hi guys,

    I've basically borrowed the code from this thread: How to display the number of posts under each tag

    This is the code I borrowed from the thread:

    <?php // List tags and post count alphabetically
        $query_string = '
    		SELECT *,name FROM '.$wpdb->prefix.'term_taxonomy
    		JOIN '.$wpdb->prefix.'terms
    		ON '.$wpdb->prefix.'term_taxonomy.term_id = '.$wpdb->prefix.'terms.term_id
    		WHERE '.$wpdb->prefix.'term_taxonomy.taxonomy = "post_tag"
    		ORDER by  '.$wpdb->prefix.'terms.name ASC';
    	$post_tags = $wpdb->get_results($query_string);
    
    	foreach($post_tags as $key => $tag) {
    		?>
    			<li><a href="<?php echo get_tag_link($tag->term_id); ?>" title="<?php echo sprintf( __( "View all posts in %s" ), $tag->name ); ?>"><?php echo $tag->name.' ('.$tag->count.')';?></a></li>
    		<?php
    	}
    ?>

    However, on a fairly large blog, the number of tags can easily run quite high, so I was wondering if it would be possible for the query to check which tags had the most number of posts attached to them, and then only display the top 10 tags?

Topic Closed

This topic has been closed to new replies.

About this Topic