A
A
Andrey2018-09-04 08:42:15
WordPress
Andrey, 2018-09-04 08:42:15

How to add date_query parameter to wp_query loop?

There is a ready-made loop that displays posts based on views (WP-PostViews plugin).

<?php
        $page = (get_query_var('page')) ? get_query_var('page') : 1;
        $temp = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query -> query('v_sortby=views&v_orderby=desc&posts_per_page=10&what_to_show=posts&post_status=publish'.'&paged='.$paged);
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
 
<!-- Тело записи  -->



<div class="post"> 
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <p><?php if(function_exists('the_views')) { the_views(); } ?></p>
</div>
    
<!-- Конец записи -->
    <?php endwhile; ?>
 

        <div class="nav">
          <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
        </div>
    
  
    <?php $wp_query = null; $wp_query = $temp; ?>

Tell me how to insert into this cycle the ability to display records by the number of views , taking into account the lifetime of the publication , for example, show only records for the last month (not older) in the cycle. I tried this guide https://misha.blog/wordpress/date_query.html , but, alas, there is not enough knowledge.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Pupkin, 2018-09-04
@dyba

after

$wp_query -> query('v_sortby=views&v_orderby=desc&posts_per_page=10&what_to_show=posts&post_status=publish'.'&paged='.$paged);

insert
$wp_query = new WP_Query(
  array_merge(
    array(
      'date_query' => array(
        'before' => array( 'year' => 2018, 'month' => 7, 'day' => 25 ),
        'after' => array( 'year' => 2018, 'month' => 7, 'day' => 25 ),
        'inclusive' => true
      )
    ),
    $wp_query->query
  )
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question