• Resolved Martin Black

    (@ownedbyhleb)


    I am currently creating a theme wherein there are two lines on navigation (top and subnav). The top layer lists the categories, and the subnav appears when you hover over a top level menu item. The sub nav then displays tags from the category in the top menu.

    In order to do this I required a custom query which loads the tags for a specific category [taken from this thread]

    So in my functions I have this code:

    function get_category_tags($args) {
    	global $wpdb;
    	$tags = $wpdb->get_results
    	("
    		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
    		FROM
    			wp_posts as p1
    			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
    			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
    
    			wp_posts as p2
    			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
    			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
    
    		WHERE
    			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
    			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
    			AND p1.ID = p2.ID
    		ORDER BY tag_name
    	");
    	$count = 0;
    	foreach ($tags as $tag) {
    		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
    		$count++;
    	}
    	return $tags;
    }

    and in my theme I simply use this to output and style the tags:

    <?php $args = array( 'categories' => '5' ); $tags = get_category_tags($args); ?>
    				<!-- echo and style tags -->
    				<?php 	$content .= "<span>";
    						foreach ($tags as $tag) {
    						$even_odd = ( 'odd' != $even_odd ) ? 'odd' : 'even';
    						$content .= "<a href=\"$tag->tag_link\" class=\"$even_odd\">$tag->tag_name</a>  |"; }
    						$content .= "</span>";
    						echo $content;
    				?>

    However, is there anyway I can modify this code so it outputs the tags in order of how many posts each tag has? I’m not overly familiar with SQL, but I believe I want to change the ORDER BY in my functions.php to sort by wp_term_taxonomy.count. Could anyone point me in the right direction?

    Thanks.

Viewing 1 replies (of 1 total)
  • Thread Starter Martin Black

    (@ownedbyhleb)

    Just in case anyone was wondering, I managed to fix this issue with this amended SQL in my functions.php

    function get_category_tags($args) {
    	global $wpdb;
    	$tags = $wpdb->get_results
    	("
    	SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link, t2.count as post_total
    		FROM
    			wp_posts as p1
    			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
    			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
    
    			wp_posts as p2
    			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
    			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
    
    		WHERE
    			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
    			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
    			AND p1.ID = p2.ID
    		ORDER BY post_total DESC
    	");
    	$count = 0;
    	foreach ($tags as $tag) {
    		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
    		$count++;
    	}
    	return $tags;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Display tags from a specific category and sort them’ is closed to new replies.