Custom template integration with WordPress popular posts plugin

Hey everyone, I’m trying to figure out how to use the WordPress Popular Posts plugin in my custom template. I want to display the most viewed posts from the last week, but I’m not sure how to combine the plugin’s function with my own query and template output.

Here’s what I’ve got so far:

// Popular posts function
if (function_exists('get_trending_content')) {
  get_trending_content('timeframe=7days&sort_by=views&post_count=5');
}

// My custom query and template
custom_post_query($args);
if (have_custom_posts()) : while (have_custom_posts()) : the_custom_post();
?>
  <article class="featured-item">
    <a href="<?php echo custom_permalink(); ?>" title="<?php custom_title_attr(); ?>">
      <?php display_featured_image(get_post_id(), 'small-thumb'); ?>
    </a>
  </article>
<?php
endwhile; endif;

Can someone help me combine these two parts? I’d like to avoid editing the plugin files or its CSS. Thanks in advance for any advice!

I’ve worked with WordPress Popular Posts before, and I can suggest an approach that might help. Instead of using the plugin’s built-in function, you can leverage its API to fetch popular posts and integrate them into your custom query. Here’s a basic example:

$instance = array(
    'limit' => 5,
    'range' => 'last7days',
    'order_by' => 'views'
);

$popular_posts = new WordPressPopularPosts\Query($instance);
$popular_ids = wp_list_pluck($popular_posts->get_posts(), 'id');

$args = array(
    'post__in' => $popular_ids,
    'orderby' => 'post__in'
);

$custom_query = new WP_Query($args);

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
        // Your custom template code here
    endwhile;
endif;
wp_reset_postdata();

This method allows you to fetch popular posts and incorporate them into your existing template structure without modifying the plugin or its CSS.

hey neo_movies, i’ve had similar issues. have you tried using the wpp_get_mostpopular() function? it lets u grab popular posts and customize the output. something like this might work:

$args = array(
    'limit' => 5,
    'range' => 'last7days',
    'order_by' => 'views'
);
$popular_posts = wpp_get_mostpopular($args);

foreach($popular_posts as $post) {
    // ur custom template code here
}

hope that helps!

I’ve tackled this issue before, and here’s what worked for me:

Instead of using the plugin’s functions directly, you can tap into the WordPress Popular Posts API. This way, you can fetch the popular posts and seamlessly integrate them into your custom template.

Here’s a snippet that should do the trick:

$popular_args = array(
    'limit' => 5,
    'range' => 'last7days',
    'order_by' => 'views'
);

$popular_query = new WordPressPopularPosts\Query($popular_args);
$popular_posts = $popular_query->get_posts();

foreach ($popular_posts as $post) {
    setup_postdata($post);
    ?>
    <article class=\"featured-item\">
        <a href=\"<?php echo get_permalink($post->id); ?>\" title=\"<?php echo get_the_title($post->id); ?>\">
            <?php echo get_the_post_thumbnail($post->id, 'small-thumb'); ?>
        </a>
    </article>
    <?php
}
wp_reset_postdata();

This approach lets you maintain your custom styling while leveraging the plugin’s popularity data. Just make sure to adjust the arguments and HTML structure to fit your specific needs.