Support » Developing with WordPress » Display popular posts from last 7 days
Display popular posts from last 7 days
-
Hi
I am using the following to display 2 posts with the most views.
I have this in my functions.php
https://pastebin.com/M3aRTB6S<div id ="latest"> <ul> <?php query_posts('meta_key=post_views_count&posts_per_page=2&orderby=meta_value_num&order=DESC'); if (have_posts()) : while (have_posts()) : the_post(); ?> /posts here/ <?php endwhile; endif; wp_reset_query(); ?> </ul> </div>
What do I need to add to only show the 2 most viewed posts from the last 7 days only?
Thanks.
-
Hey makta112,
I’d highly suggest converting the query_posts function over to a WP_Query and using the date parameters – https://codex.wordpress.org/Class_Reference/WP_Query
You can use the after parameter to do something like:
'date_query' => array( array( 'after' => '7 days ago', // or '1 week ago' 'inclusive' => true, ), ),
Hope this helps
Hi Adam
Thanks for your reply.Can you please show me the exact code I need to replace the current one with? I’m not code savvy at all so please do explain things as you would to a beginner.
Thanks again.
Hey makta112,
I can’t fully test this at the moment but I would of thought something like:
<?php //Set the parameters / arguments for the query $popular_post_args = array( 'post_type' => 'page', // The post type you need to query, can remove if it's just posts 'meta_key' => 'post_views_count', //meta key currently set 'orderby' => 'meta_value_num', //orderby currently set 'order' => 'DESC', //order currently set 'posts_per_page' => 2, // show 2 posts 'date_query' => array( // date query from after 1 week ago array( 'after' => '1 week ago', ), ), ); //Initialise the Query and add the arguments $popular_posts = new WP_Query( $popular_post_args ); //Check if theres posts and add your html divs around it if ( $popular_posts->have_posts() ) : ?> <div id="latest"> <ul> <?php //Start the loop while ( $popular_posts->have_posts() ) : $popular_posts->the_post(); ?> <?php //Whatever goes here, show title for example the_title(); ?> <?php endwhile ?> <?php //Reset back to the normal loop wp_reset_postdata(); ?> </ul> </div> <?php endif; ?>
Would work. Please only test this on a local or staging environment though if you’re not comfortable implementing this on a live server.
Many thanks
Thanks. I will try this π
It didn’t work.
Nothing shows up when I add it to the sidebar…Hey @makta112,
Can you try removing the ‘post_type’ => ‘page’ argument or setting it to post (it will default to posts anyway).
Many thanks
It doesn’t show up…
Just to be sure.
Do I have to make any changes to what I have in my functions.php?
Or
The counter which I have in single.php?
<?php setPostViews(get_the_ID()); ?>
Finally, the code that you’ve posted goes in the sidebar where I want it to show up, yes?
Thanks!
^ also I tried removing the post_type part.
Hey @makta112,
That sounds right to me.
The function and call you have are just adding to the meta field which is stored in the database.
With the WP_Query we are just querying all the posts with that field.
I’ve just tested it on a similar scenario and it seems to work fine for me.
What are you doing inside the loop by chance? The bit where you specified “/posts here/”
Many thanks
Hi @adam3128
I put this inside the loop:
<li> <a href="<?php the_permalink() ?>"> <div id="full-thumb"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'news-thumb' ); } else { ?> <img src="/wp-content/uploads/2017/05/fallback-350x200.jpg" alt="<?php the_title(); ?>" /> <?php } ?> </div> <div id="latest-entry-title"> <?php the_title(); ?> </div> </a> </li>
Thanks π
Hey Mason,
That looks fine to me.
Sorry it’s quite hard to think of what could be wrong now without testing / debugging the theme.
Have you definitely got posts that have been created in the last 7 days?
Is this the intention? or are you looking to reset the counter every 7 days? So even old posts could be “popular” if they’ve been hit so many times?
I just made a test post and it showed up!
As you point out, the intention was actually to show old and new posts and for the counter to reset every week. But if it’s too much trouble to do that, I will use the code that you’ve already provided.
Thanks again. I really appreciate your help! π
Mason
Hey makta112,
Glad you got it working with the Date Query.
If you were looking for the counter to reset each week you’d be looking at probably having to set up a cron job that runs and deletes the data inside those meta fields.
You might be better off creating a new meta field though so you can still keep a record of the total views in case you ever wanted to use though.
It would certainly be a bit more involved though. You could always use the above to show the “Latest Trending” posts and remove the Date Query to show the all time “Popular” Posts or something?
All the best
- The topic ‘Display popular posts from last 7 days’ is closed to new replies.