3

I'm trying to sort the popular posts so it shows the most visited in the last week, but it hasn't worked. Anyone have an idea of why it isn't working?

<?php 

$popularpost = new WP_Query( array (

        'posts_per_page'      => 5,
        'ignore_sticky_posts' => 1,
        'meta_key'            => 'sw_post_views_count',
        'orderby'             => 'meta_value_num',
        'order'               => 'DESC',
        'date_query'          => array (
                array (
                        'year' => date( 'Y' ),
                        'week' => date( 'W' ),
                ),
        ),
) );

while( $popularpost->have_posts() ) :
       $popularpost->the_post(); ?>
  • Please share some of the symptoms of your posted code "not working" - what's happening that isn't supposed to? Review the "How to Ask" page of our help center for more tips to improve your question. – bosco Jan 31 '16 at 8:49
  • is this the php file of your plugin ? – MMKY Dec 3 '18 at 13:11
2

Use strtotime to compare dates.

$start = strtotime('yesterday');
$end = strtotime( '+1 week',$start);

$args = array(
    'posts_per_page' => 5,
    'ignore_sticky_posts' => 1,
    'meta_key' => 'sw_post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'date_query' => array(
        'after' => $end,
        'before' => $start,
    ),
);

$popularpost = new WP_Query( $args );

if ( $popularpost->have_posts() ) {

    while ( $popularpost->have_posts() ) {
        $popularpost->the_post();

        // Do your stuffs

    }

} 

Please note, this will return the posts from last 7 days, not last week.

  • Hey, so it won't show the most viewed from the current week, but the week before. Did i get this correctly? – Max Feb 1 '16 at 0:06
  • @Max Yes, that's right. – Abhik Feb 1 '16 at 3:39
  • ok, but that's not what i need. I need from the current week, or for an example from the last 10 days will work as well..any idea for that? – Max Feb 1 '16 at 19:57
  • @Max I've edited my answer and changed the code to retrieve posts from last 7 days. If that solved your issue, please accept it as the answer. – Abhik Feb 2 '16 at 5:22
  • Awesome, thank you. I've seen that a lot of people search for this, hopefully it will help them as well. Kind regards – Max Feb 3 '16 at 0:18
-2

If you are looking for plugin then this is simple way to install Plugin: WordPress Popular Posts

Add Shortcode to sidebar:

  [wpp range=weekly stats_comments=0 thumbnail_width=30 thumbnail_height=30 thumbnail_selection=usergenerated do_pattern=1 pattern_form={image}]
  • Personally when making your own theme I would try a build most of the little things like this into your theme just so then it is as heavy on your site – Stephen Jan 31 '16 at 12:16
  • sorry,I didn't get you – Vasim Shaikh Jan 31 '16 at 12:25
  • 2
    Something as small as this like showing popular posts shouldn't require a Plugin because it's such a small thing to do and doesn't require much installing a Plugin for this is in my opinion pointless – Stephen Jan 31 '16 at 12:41
  • okay,did you get a solution or I am find interesting in this question.I want to try – Vasim Shaikh Jan 31 '16 at 12:45
  • Oh no I didn't ask the question xD I was just giving my opinion on why he shouldn't use a Plugin to do this - my bad I thought you knew I wasn't asking the question, anyways have a good day (or night) Sir :) – Stephen Jan 31 '16 at 12:45

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.