0

I am using the snippet below to create a shortcode to get all tags for a search in a separate list including tag count. Eg search for 'foo' and it returns all tags for all posts that match the search 'foo'.

I am trying to also get the categories from the same search in a separate list using a second shortcode from the same global search. I am not sure how I can get the categories from the same global search.

Eg search for for 'foo' and get:

Tag: Tag1 (10), Tag2 (20), Tag3 (2) etc (this is the shortcode and already works)

Category: Cat1 (10), Cat2 (2) etc. (not sure how to get the second shortcode to work)

    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 style="background-color:#fffff; border-radius:20px; box-shadow: 0 3px 5px 0 rgb(0 0 0 / 8%); font-weight: bold; color:#6c757d; padding:8px; margin:5px;" href="'.$link.'">' . __( $term->name ) ." ($count)". '</a>'.'';
            
        }
    }

    return '<div style="height:50px; padding-top:10px; overflow-x:auto; white-space: nowrap;">'.implode( $sep, $links ).'</div>';
}

Shortcode

add_shortcode( 'bd_terms_list', 'bd_terms_list_custom_callback');
function bd_terms_list_custom_callback( $args ){
    return get_query_terms_list( 'gd_place_tags' );
}
0

how I can get the categories from the same global search

You can add a taxonomy attribute to your shortcode and then pass the value to get_query_terms_list(), like so:

add_shortcode( 'bd_terms_list', 'bd_terms_list_custom_callback');
function bd_terms_list_custom_callback( $args ){
    $atts = shortcode_atts( array( // list of default attributes
        'taxonomy' => 'gd_place_tags',
        'sep'      => '', // optional, but you might use it someday? :)
    ), $args );

    return get_query_terms_list( $atts['taxonomy'], $atts['sep'] );
}

See the Shortcodes API on Codex for more details about handling shortcode attributes.

And for example with the default post_tag and category taxonomies included in WordPress core, you can display the tags and categories in separate list like so:

Tags: [bd_terms_list taxonomy="post_tag"]

Categories: [bd_terms_list taxonomy="category"]

So you really just need to use the correct taxonomy slug, just like you could see above.

In response to your comment:

the slug for category is 'gd_postcategory' without the underscore for some reason. I'm not sure how to fix the underscore, so I added 2 x shortcodes now instead of using the more elegant taxonomy="category". It works :)

Why would you need to "fix the underscore"?

And you do know the differences between taxonomies and terms, don't you? (see https://developer.wordpress.org/themes/basics/categories-tags-custom-taxonomies/)

So for example, if I registered custom "tag" and "category" taxonomies named gd_place_tags and gd_postcategory respectively, then I would display the tag/category list like so:

Tags: [bd_terms_list taxonomy="gd_place_tags"]

Categories: [bd_terms_list taxonomy="gd_postcategory"]

But if you're absolutely certain the above just doesn't work for you, then yes of course, you could just create another shortcode which calls the get_query_terms_list() function. :)

However, if I were you, I would try again with the modified bd_terms_list_custom_callback() function as well as the HTML code above, but I would double-check that I'm using the correct taxonomy slugs (and not the label like "My Taxonomy" or even the slug of a term in the taxonomy like my-category-in-my-taxonomy).

5
  • Hi, Thank you for the prompt reply :) This makes sense to me but I can't get it to work. I added the above snippet to the functions.php and used the shortcode but I can't get it to work. How does the shortcode get the 'catgeories' or 'tags'? Sorry, this might be a super obvious thing that I am missing.
    – RobH
    Sep 19 at 11:21
  • It sounds like you just need to correct the taxonomy. So the default taxonomy value in the shortcode is gd_place_tags and therefore you could simply do [bd_terms_list]. But for other taxonomies, you need to use the taxonomy attribute like so: [bd_terms_list taxonomy="my_taxonomy"]. So just replace the my_taxonomy with the correct taxonomy slug, like gd_place_categories, maybe?
    – Sally CJ
    Sep 19 at 11:54
  • Thank you :) I found the issue after reading your last post. the slug for category is 'gd_postcategory' without the underscore for some reason. I'm not sure how to fix the underscore, so I added 2 x shortcodes now instead of using the more elegant taxonomy="category". It works :) Thank you so much for your reply. This is the second time you actually assist me with this. Thank you :)
    – RobH
    Sep 19 at 20:27
  • I've added that to my answer and I'd appreciate if you can accept it? Or at least read it carefully and let me know what you think? :)
    – Sally CJ
    Sep 20 at 3:07
  • 1
    I missed that :) Thank you. That fixed the issue and I don't need to create a second shortcode. Thank you again for following this up and updating the last post. This made it very clear. Very silly I missed this :)
    – RobH
    Sep 21 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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