« back

Get page tag id’s based on a category in WordPress

For the new threeformed site I am using WordPress as a light CMS, so I wanted to treat some ‘special’ posts differently than others based on their category. This meant that i’d have to exclude these ‘special’ posts from a few places, one of these places was the tag cloud.
Hmmm, I can only exclude tags from

wp_tag_cloud()

based on ID’s? So that seemed easy enough right?! I could just use

get_the_tag_ids('exclude=special')

… Well if that function existed. Well it does now!


// Gets tags ID's for current post
	function get_the_tag_ids($which, $delimiter) {
		// Gets posts for the category
		query_posts('category_name='.$which);
		// Loop through each post with $which category
		while(have_posts()) {
			the_post();
			// Get the tags for each post
			$this_post_tags = get_the_tags();
			// if there are tags
			if ($this_post_tags) {
				// Get the tag ID for each
				foreach($this_post_tags as $tag) {
					$taxarray = is_term($tag->name, 'post_tag');
					$post_tags .= $taxarray[term_id].$delimiter;
				}
			}
		}
		wp_reset_query(); // Resets the query
		return $post_tags;
	}

Usage

get_the_tag_ids('special',',')

Output (example):

8,3,9,8,12
No comments, leave one...

 
 Leave a Comment 


Archived Posts

Linkage