Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ..

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

Use get_terms().

share|improve this answer
    
no I want to using a string sql query . thanks –  AboSami Jul 2 '11 at 22:40
    
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 Jul 2 '11 at 23:05
    
okey , but when I wrote get_terms('post_tags'); it dosen't work , just work with category –  AboSami Jul 2 '11 at 23:07
1  
The correct taxonomy name is 'post_tag'. –  scribu Jul 3 '11 at 0:01
    
Also you'll want to write echo $tag->name. –  scribu Jul 3 '11 at 0:02
show 1 more comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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