Return all Tags from search results

The global $wp_query object is available before you call have_posts(). All you have to do is running through this object’s posts member with get_the_terms().

Sample function:

function get_query_terms( $taxonomy = 'post_tag' )
{
    $list = array ();

    foreach ( $GLOBALS['wp_query']->posts as $id )
    {
        if ( $terms = get_the_terms( $id, $taxonomy ) )
        {
            foreach ( $terms as $term )
                $list[ $term->term_taxonomy_id ] = $term;
        }
    }

    ksort( $list );

    return $list;
}

Be aware any usage of query_posts() might break this list.

Leave a Comment