I want a query to list tags

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

Solutions Collecting From Web of "I want a query to list tags"

Use get_terms().

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.