Evgeny Evgeny - 5 months ago 32
PHP Question

Getting tags related to posts only from selected category in Wordpress

I want to get tags related to posts only from selected category using get_terms or something. Is it possible?

Answer Source

It is not actually bound to category, but to posts and their IDs from that category. So you should be probably getting list of all post IDs in array from results of current query for category index page ( take a look at category.php, or archive.php template for the query loop) Example:

    $post_ids = array(); 
    while (have_posts()) : 
        $post_ids[] = get_the_ID(); 

. And use that to call wp_get_object_terms($post_ids, 'post_tag'), to retrieve all post tags for given post ids from above.

It could be done building a custom db query with specific joins, which could be better for performance, but this is one way to do.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download