0

I want to display tag clouds for specific year's posts to summarize year in post.

https://codex.wordpress.org/Function_Reference/wp_tag_cloud has no year filter. How to get it?

1 Answer 1

1

All you can do is to find the post from current year and filter out the tags from those posts. Please see the code below,

$getdate = getdate();
$args = array(
    'date_query' => array(
        array(
            'year'  => $getdate["year"]
        ),
    ),
);
$query = new WP_Query( $args );

$years_tag = array();

if ( $query->have_posts() ): 

    while ( $query->have_posts() ) : 

        $query->the_post();

        $post_tags = wp_get_post_tags( get_the_id(), array( 'fields'=>'ids' ) );

        foreach ( $post_tags as $tag) {

            if( !in_array( $tag, $years_tag ) ) {
                array_push( $years_tag, $tag );
            }
        }

    endwhile; 
endif;

$args = array(
    'include' => implode(',', $years_tag),
);

wp_tag_cloud( $args );

You can accurately get tags from this year BUT the main issue here is the count of tags is not based on this year. You can manipulate the array to bring the count along with tag ids and create your own function on that.

I hope this will be somewhat helpful to you.

2
  • include has to be a comma separated string
    – inarilo
    Dec 30, 2017 at 6:51
  • Oh right, i mistaken it to be an array. implode should be used to make it string Dec 30, 2017 at 7:04

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.