cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
280 stars 83 forks source link

How do I display a list of tags from popular posts? #193

Closed ghost closed 5 years ago

ghost commented 6 years ago

how do I display a list of tags from popular posts?

get_tags()

cabrerahector commented 6 years ago

Hi @broros,

~Well, assuming you're using the WPP widget:~

~1. Go to Widgets > WordPress Popular Posts.~ ~2. Under Stats Tag settings, tick the Display taxonomy checkbox and hit the Save button to apply changes.~ ~3. You'll see some new options appear under the Display taxonomy checkbox. Select Tag and hit the Save button once again to apply changes.~

~That's it!~

Please scratch all that. I'm still half-asleep.

If you want to get the tags used on your popular posts, I'd do something like this:

<?php
// Get popular posts
$args = array(
    'limit' => 10,
    'range' => 'last7days'
);
$popular_posts = new WPP_Query( $args );

if ( $popular_posts->get_posts() ) {

    $tags = array();

    foreach( $popular_posts->get_posts() as $popular_post ) {

        $post_tags = get_the_tags( $popular_post->id );

        foreach( $post_tags as $tag ) {
            if ( ! isset( $tags[$tag->slug] ) ) {
                $tags[$tag->slug] = array(
                    'id' => $tag->term_id,
                    'name' => $tag->name
                );
            }
        }

    }

    var_dump( $tags );

}