如何通过比较ACF自定义字段日期值来过滤最近一年的自定义帖子

时间:2019-07-03 05:30:34

标签: wordpress meta-query

我只能通过比较acf定制字段datepicker值来获取最近12个月的最新帖子

$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 个答案:

答案 0 :(得分:0)

您可以通过以下方式获得上一年的职位:

$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,
            )
      )

 );

在这里,我们可以使用元查询来获取它。

答案 1 :(得分: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>',
            )
        );
        ?>