0

I am trying to create a list of tags on top of my search results page that returns all tags and tag count for all tags that are included in the search results.

For example, I search for 'Foo' and it returns 100 posts all with different tags eg pizza, bred, tomatoes. I would like all the tags with tag count (tag count related to search result) related to my search to show on top of my search results page eg pizza (50), bred (10), tomatoes (5).

I played around with two approaches but can not get this two work.

APPROACH 1 Return all Tags from search results

    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;
}

APPROACH 2

function list_search_tags() {
   $tagTmp = array();
   $tagLinks = '';
  
   // Start The Loop
      while (have_posts()) : the_post();
         $post_tags = get_the_tags();

         foreach ($post_tags as $tag) : $tagID = $tag->term_id;

            if (empty($tagTmp[$tagID])) :
               $tagTmp[$tagID] = 1;
               $tagURL = get_tag_link($tagID);
               $tagName = $tag->name;
               $tagLinks .= '<li><a class="tag" href="' . $tagURL . '">' . $tagName . '</a></li>';
            endif;
  
      endforeach;
   endwhile;

   echo $tagLinks;}

Thanks :)

1 Answer 1

0

Try this one:

Just add the function in your theme functions file and then in your (search) template, add echo get_query_terms_list(); to display the terms list. But do take note, this is intended only for the current search results, i.e. on the same page.

function get_query_terms_list( $taxonomy = 'post_tag', $sep = ', ' ) {
    $list = array();

    foreach ( $GLOBALS['wp_query']->posts as $post ) {
        if ( is_array( $terms = get_the_terms( $post, $taxonomy ) ) ) {
            foreach ( $terms as $term ) {
                // Set the term's initial count.
                if ( ! isset( $list[ $term->term_id ] ) ) {
                    $list[ $term->term_id ] = 0;
                }

                // And then increment it for each post.
                $list[ $term->term_id ]++;
            }
        }
    }

    // Sort by the count, highest to lowest.
    arsort( $list );

    $links = array();

    foreach ( $list as $term_id => $count ) {
        $term = get_term( $term_id );
        $link = get_term_link( $term );

        if ( ! is_wp_error( $link ) ) {
            $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' .
                $term->name . " (<b>$count</b>)" . '</a>';
        }
    }

    return implode( $sep, $links );
}
1
  • 1
    Awesome :) Works perfectly. Thank you so much!! Sometimes I think internet forums are amazing, where people share their knowledge just to help others :) I really appreciate this. Thanks again for sharing your knowledge here :) You made my day!!
    – RobH
    Commented Sep 4, 2021 at 23:46

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.