最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - Sort Popular Posts by Views for the Last Week

programmeradmin2浏览0评论

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(); ?>

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(); ?>
Share Improve this question edited Feb 2, 2016 at 6:25 jgraup 9,9143 gold badges32 silver badges70 bronze badges asked Jan 31, 2016 at 3:38 MaxMax 1294 silver badges14 bronze badges 2
  • 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 Commented Jan 31, 2016 at 8:49
  • is this the php file of your plugin ? – Mohammad Mahdi KouchakYazdi Commented Dec 3, 2018 at 13:11
Add a comment  | 

2 Answers 2

Reset to default 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.

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}]
发布评论

评论列表(0)

  1. 暂无评论