Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP How To Get WordPress Tags By Categories

How To Get WordPress Tags By Categories

The PHP code example will execute a custom SQL Query in WordPress to get tags specific from categories. Simply add the function below in your theme’s functions.php file or somewhere in your custom page or plugin. It accepts multiple category ids as input parameter and then return an array of tags.

Also in this tutorial, I’ll show a similar way to display the list of tags.

[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
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;
}
[/php]

Usage Example

[php] <?php
$args = array(
‘categories’ => ‘12,13,14’
);
$tags = get_category_tags($args);

$content .= "<ul>";
foreach ($tags as $tag) {
$content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
}
$content .= "</ul>";
echo $content;

?>
[/php]

Or, you can try with the code below:

[php] <?php
$project_query = query_posts(‘category_name=projects’);
while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
}
}
endwhile;
?>
<?php if ( is_array($all_tags_arr) && count($all_tags_arr) > 0 ): ?>
<?php
$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
foreach( $tags_arr as $tag ):
$el = get_term_by(‘name’, $tag, ‘post_tag’);
$arr[] = ‘"tag-‘.$el->slug.’"’;
?>
<span><a href="#<?php echo $el->slug; ?>" id="taglink-tag-<?php echo $el->slug; ?>" rel="tag-<?php echo $el->slug; ?>"><?php echo $el->name; ?></a> <span class="slash">//</span></span>
<?php endforeach; ?>
<?php echo ‘<pre>’.print_r($tags_arr, true).'</pre>’;?>
[/php]

I copied the source code above from WordPress Support Get Tags specific to Category

May 19, 2011Hoan Huynh
Sending Email With SMTP Authentication In WordPressTruncate And Limit Displayed Text With Selected Maximum Length PHP Function
You Might Also Like:
  • WordPress Delete Unused Post Tags By SQL Command
  • Manage Or Organize WordPress Media Library Structure In Categories
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
  • Function Remove All HTML Tags In PHP
  • Change Favicon In Your WordPress Blog Or Website
  • Show Error Message On WordPress Custom Login Template Page
  • Add WordPress Login Form On Sidebar Or Custom Page
  • Get WordPress Short Link And Full Link To A Post
  • Generate And Display Related Content On WordPress
  • Add More Extra Informations Or Fields To WordPress User Profile
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHP, Tips And Tricksget_results, get_tag_link, get_the_tags, How To, query_posts, Wordpress278
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,195 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,069 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,846 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,323 views
C# Read Json From URL And Parse/Deserialize Json
9,815 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development