0

how are you?

I want to using a string query to list tags in array and using it.

I write this query but dosen't work

$tags= $wpdb->query("SELECT * FROM $wpdb->term_taxonomy , $wpdb->term_taxonomy WHERE $wpdb->term_taxonomy.taxonomy = post_tag");                    

foreach($tags as $tag){
echo $tag;
}

please help me ..

2 Answers 2

5

Use get_terms().

4
  • 1
    No, you don't want to use a raw SQL query. It's always better to use the functions that come with WordPress, when they're available: better performance, future compatibility etc.
    – scribu
    Commented Jul 2, 2011 at 23:05
  • okey , but when I wrote get_terms('post_tags'); it dosen't work , just work with category
    – AboSami
    Commented Jul 2, 2011 at 23:07
  • 1
    The correct taxonomy name is 'post_tag'.
    – scribu
    Commented Jul 3, 2011 at 0:01
  • Also you'll want to write echo $tag->name.
    – scribu
    Commented Jul 3, 2011 at 0:02
3

This query (original can be found here – I use it to make a sitemap from tags) lists ALL TAGS sorting by their last appearance in published posts (on the top you will find more recently used tags, despite their total count/popularity):

$wpdb->get_results("SELECT
name,
slug,
tag_history.tagdate AS lastmod FROM
(SELECT
$wpdb->term_relationships.term_taxonomy_id AS tagid,
substr($wpdb->posts.post_date_gmt,1,10) AS tagdate FROM $wpdb->term_relationships
INNER JOIN
$wpdb->term_taxonomy ON 
$wpdb->term_taxonomy.term_taxonomy_id=$wpdb->term_relationships.term_taxonomy_id
INNER JOIN
$wpdb->posts ON $wpdb->posts.ID=$wpdb->term_relationships.object_id
WHERE taxonomy='post_tag'
AND post_status='publish'
AND description!=''
ORDER BY
post_date_gmt DESC
) AS tag_history
INNER JOIN
$wpdb->terms ON $wpdb->terms.term_id=tag_history.tagid
GROUP BY
tag_history.tagid
ORDER BY
tag_history.tagdate DESC");

Remember that if you don't delete..

AND description!=''

..from query you wil get ONLY tags with description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.