【问题标题】:How to filter last one year custom posts by comparing acf custom field date value如何通过比较acf自定义字段日期值过滤过去一年的自定义帖子
【发布时间】:2019-11-13 18:00:18
【问题描述】:

我必须通过比较 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, 
 );

【问题讨论】:

    标签: wordpress meta-query


    【解决方案1】:

    您可以通过以下方式获取上一年的帖子:

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

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

    【讨论】:

    • 非常感谢代码正在运行。但是所有超过一年的帖子也将分页。
    • 我只需要为一年的帖子进行分页。
    • 您是否使用了页面模板或者您是如何以及在何处编写此代码的?
    • 我正在使用自定义存档页面模板archive-news.php
    • 好的,只需通过分页功能传递查询参数即可。请参考developer.wordpress.org/reference/functions/…了解更多信息,
    【解决方案2】:
    <?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>',
                )
            );
            ?>
    

    【讨论】:

      猜你喜欢
      • 2016-01-11
      • 1970-01-01
      • 2017-05-10
      • 2013-02-09
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      相关资源
      最近更新 更多