Search code examples
phpwordpresstagsshortcode

Show the tag if the custom field is set to “Yes”


Using the code below I output 10 tags in the right place using the shortcode [tagsListfoot]:

function getTagListfoot($classes = '') {
    global $post;
    wp_tag_cloud( array( 'taxonomy' => 'tags_type', 'order' => 'RAND', 'number' => '10' ));
    $tagOutput = [];

    if (!empty($tags)) {
        array_push($tagOutput, '<ul class="tag-list '.$classes.'">');
        foreach($tags as $tag) {
            array_push($tagOutput, '<li>'.$tag->name.'</li>');
        }
        array_push($tagOutput, '</ul>');
    }

    return implode('', $tagOutput);
}

add_shortcode('tagsListfoot', 'getTagListfoot');

Each tag has an additional custom field "Output in the basement?" with a drop-down list of "Yes" and "No". I tried to make a check for compliance with "Yes" before displaying the tag using get_term_meta, like that:

if ( 'yes' == get_term_meta('pokazat-v-podvale') ):
 array_push($tagOutput, '<li>'.$tag->name.'</li>');
endif;

But I can't get the desired result. All tags are still displayed, including those with a "no" value. Is it possible to do this here?

I will be grateful for any help in this!

....

add_shortcode( 'tagsListfoot', 'getTagListfoot' );
    function getTagListfoot() {

    wp_tag_cloud( array( 
        'taxonomy'  => 'tags_type',
        'order'     => 'RAND',
        'number'    => '10',
    ) );
}

It seems that the bloated code did not make any difference at first, it also works if it is simplified to simple wp_tag_cloud


Solution

  • Just solved it for the same query!

    Try this:

    add_shortcode( 'tagsListfoot', 'getTagListfoot' );
        function getTagListfoot() {
        global $post;
        
        $args = array( 
            'taxonomy'  => 'tags_type',
            'order'     => 'RAND',
            'number'    => '10',
            'meta_query' => array(
            array(
                'key'     => 'pokazat-v-podvale',
                'value'   => 'yes',
                'compare' => 'IN',
            ),
        ),
        );
        wp_tag_cloud( $args );
    }