2

I have to get latest posts from only last 12 months by comparing the acf custom field datepicker value

$args = array( 
  'post_type' => 'news',
  'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
   'meta_query' => array(
array(
    'key' => 'publication_date',
     'after'   => '-365 days',
 ),
 ),
 'orderby' => 'meta_value',
  'order' => 'DESC',
  'hide_empty' => 0,
   'pad_counts' => false, 
 );

2 Answers2

2

You can get the post of last a year by:

$today = date('Ymd'); // Today's date
$date = strtotime($today.' -1 year'); // converting into string and doing minus a year
$lastyear = date('Ymd', $date); //get the date of the last year

$args = array( 
  'post_type' => 'news',
  'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
  'orderby' => 'meta_value',
  'order' => 'DESC',
  'hide_empty' => 0,
  'pad_counts' => false,

    'meta_query' => array(
      'relation' => 'AND',
           array(
                'key'       => 'publication_date',
                'compare'   => '<=',
                'value'     => $today,
            ),
               array(
                'key'       => 'publication_date',
                'compare'   => '>=',
                'value'     => $lastyear,
            )
      )

 );

Here we can fetch it using meta query.

s45 Dev
  • 111
  • 8
  • Thanks alot the code is working. but the pagination is coming for all the posts older than one year also. – Vignesh Thangavel Jul 03 '19 at 06:19
  • I need pagination for only one year posts. – Vignesh Thangavel Jul 03 '19 at 06:39
  • Are you using a page template or how and where you are writing this code? – s45 Dev Jul 03 '19 at 07:02
  • I'm Using custom archive page template archive-news.php – Vignesh Thangavel Jul 03 '19 at 07:04
  • Okay, just pass the query argument with pagination function. Please refer https://developer.wordpress.org/reference/functions/the_posts_pagination/ for more info, – s45 Dev Jul 03 '19 at 07:24
  • the_posts_pagination() should work with your code. Debug with Today and Last Year variables and it will give you the exact idea. – s45 Dev Jul 03 '19 at 07:30
  • the pagination is coming for all the post count – Vignesh Thangavel Jul 03 '19 at 12:05
  • If this code is not working then you can count a total number of pages and can create custom pagination. Eg: If there are 20 posts and per page, and you want to show 5 posts then there will be 4 pages so just check which page you are at and create custom page link like if on next you can add link domain.com/page/3 – s45 Dev Jul 04 '19 at 04:51
0
<?php   
            $today = date('Ymd'); // Today's date
            $date = strtotime($today.' -1 year'); // converting into string and doing minus a year
            $lastyear = date('Ymd', $date); //get the date of the last year
            $args = array( 
              'post_type' => 'news',
              'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
              'orderby' => 'meta_value',
              'order' => 'DESC',
              'hide_empty' => 0,
              'pad_counts' => false,

                'meta_query' => array(
                  'relation' => 'AND',
                       array(
                            'key'       => 'publication_date',
                            'compare'   => '<=',
                            'value'     => $today,
                        ),
                           array(
                            'key'       => 'publication_date',
                            'compare'   => '>=',
                            'value'     => $lastyear,
                        )
                  )

             );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <h4 class="common_heading4 site_clr text_transform"><?php the_title(); ?></h4>
            <p class="mb-0"><?php echo the_field('news_short_content'); ?></p>
            <a class="download_pdf" href="<?php the_permalink(); ?>" >Read More</a>
        <?php endwhile; ?>
        <?php
        // Previous/next page navigation.
        the_posts_pagination(
            array(
                'prev_text'          => __( 'Previous page', 'theme' ),
                'next_text'          => __( 'Next page', 'theme' ),
                'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'theme' ) . ' </span>',
            )
        );
        ?>