-1

I have sidebar.php and it show 10 popularpost of my wordpress post. But i want just Display popularpost of the last 2 days. How i can correct it?

Thanks

                    </div>
                <?php } if (of_get_option('most_radio')==1) { ?>
                 <div class="rmenu col-lg-12 col-sm-12 col-xs-12">
                    <header class="title-panel">
                        <div class="title-arrow"></div>
                        <h2 class="title-text"><?php echo of_get_option('most_title'); ?></h2>

                    </header>
                    <footer class="most-viewed">
                        <div class="most-viewed-content mCustomScrollbar" data-mcs-theme="dark-thin">

                            <?php if(of_get_option('most_radio_select')==1){
                                    $popularpost = new WP_Query( array(
                                    'post_status' =>'publish',
                                    'post_type' =>'post',
                                    'posts_per_page' => of_get_option('most_count'),
                                    'orderby' => 'meta_value_num',
                                    'meta_key' => 'post_views_count',
                                    'order' => 'DESC'
                                    )
                                ); ?>
                                <ol>
                            <?php
                                if($popularpost->have_posts()) : while($popularpost->have_posts()) : $popularpost->the_post();
                            ?>

                                    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> - <?php echo getPostViews(get_the_ID()); ?> </li>

                            <?php endwhile; endif; ?></ol> <? }else{?>

                            <ol>
                            <?php
                                $big_query = new WP_Query(array(
                                'post_status' =>'publish',
                                'post_type' =>'post',
                                'cat' => of_get_option('most_select_categories'),
                                'posts_per_page' => of_get_option('most_count'))); ?>
                                <?php if($big_query->have_posts()) : while($big_query->have_posts()) : $big_query->the_post();?>

                                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                <?php endwhile; ?>
                    <?php endif; ?>

                            </ol>
                            <?php } ?>

0

in argument list please also pass date_query, try below example.

$args = array(
    'posts_per_page' => 10,
    'post_type' => 'post',
    'order' => 'DESC',
    'date_query' => array(
        'after'     => '2 days ago',  
        'inclusive' => true
    )
); 
$posts = get_posts($args);
  • I try use date_quer , But my problem is not solved. I think it is my fault. can you edit original file? thanks for your help – Ms Hannibal Apr 24 '17 at 7:33
1

I think you forgot to add date_query argument in your query, please check below example.

$args = array(
'posts_per_page' => of_get_option('most_count'),
'post_type' => 'post',
'orderby' => 'meta_value_num',
'meta_key' => 'post_views_count', 
'order' => 'DESC',
'post_status' =>'publish', 
'date_query' => array(
    'after' => date('Y-m-d', strtotime('-2 days')) 
));

 $posts = get_posts($args);

As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

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.