• Resolved janvitos

    (@janvitos)


    Hi,

    Thanks for this awesome plugin, it’s really one of a kind and so useful.

    I’m using the widget on the bottom of my articles to show most popular posts for a given time period, and it works great. Though, I wanted to exclude the current post from top posts so it wouldn’t be redundant to the user. This is the code I’m using:

    [google_top_content pageviews=5 number=6 showhome=no time=604800 timeval=1 thumb_size=medium postfilter='. $post->ID .']

    This works great and does what I need, but there’s a small problem. There are now tons of unique transients being created. And since almost every post has its own transient, this has a negative impact on response time because of all the requests being made to Google Analytics (and it kind of defies the purpose of transients altogether).

    I think a better solution would be to only remove the current post if it’s a popular post (and not from all posts) and use the same transients for the rest. The only thing is, I have no idea if this is feasible with your plugin, and if so, how one could code it.

    If you think there’s a way to exclude the current post only if it’s one of the popular posts, I would happily like to know.

    Thank you,

    Janvitos

    • This topic was modified 8 years, 6 months ago by janvitos.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter janvitos

    (@janvitos)

    For anyone interested, I actually found out how to do it.

    Here is the code I’m using in my single_post.php file:

    <?php
    $top_content = do_shortcode('[google_top_content pageviews=5 number=6 showhome=no time=604800 timeval=1 thumb_size=medium]');
    $top_content_url_found = strpos($top_content, $_SERVER['REQUEST_URI']);
    if ($top_content_url_found === false) {
        echo $top_content;
    } else {
        echo do_shortcode('[google_top_content pageviews=5 number=6 showhome=no time=604800 timeval=1 thumb_size=medium postfilter='. $post->ID .']');
    }
    ?>

    So essentially, what this does it trigger the shortcode and check if the current URL is there. If it’s not, then just return the shortcode. If the current URL is found, then remove the post ID, generate a new transient and return the shortcode. This way, only different transients will be created for the top posts.

    • This reply was modified 8 years, 6 months ago by janvitos.
    Plugin Author Justin Sternberg

    (@jtsternberg)

    Glad you got it sorted, and thank you for sharing.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude current post from top posts’ is closed to new replies.