duangai1941 2016-05-13 10:42
浏览 62
已采纳

ACF - 如何从关系字段中排除超过一年的帖子?

I am using ACF Pro. I have a filter setup that tells ACF only to show posts that are published.

function acf_relationship_filter( $args, $field, $post_id ) {
    $args['post_status'] = array('publish');
    return $args;
}

add_filter('acf/fields/relationship/query/name=featured_items', 'acf_relationship_filter', 10, 3);

This works just fine. However, I'd like to extend this function so that only posts made in the last year are shown; older posts should therefore be excluded.

Anyone have ideas on how to do this?

I need to somehow apply the following test in the filter:

strtotime( $args['post_date'] ) > strtotime('-365 day');

Comparing the result of this on $args['post_date'] wont work of course because I'd be comparing a boolean result to a date string.

Thanks!


SOULTION:

I figured this out for anyone who might come across this same requirement.

Simply use $args['date_query'].

function acf_relationship_filter( $args, $field, $post_id ) {

    $args['post_status'] = array('publish');
    $args['date_query'] = array(
        'after' => date('Y-m-d G:i:s', strtotime('-1 year')),
        'inclusive' => true
    );

    return $args;

}

add_filter('acf/fields/relationship/query/name=featured_items', 'acf_relationship_filter', 10, 3);

This will only show the posts that were made after a year ago from today.

Hope it helps someone out there!

WP Docs on date parameters for WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

  • 写回答

1条回答 默认 最新

  • dongshi7433 2016-05-13 11:13
    关注

    If these are arguments to WP_Query, then you could try the date_query input argument:

    $args['date_query'] = [
        [
            'after'     => '1 year ago', // <-- your strtotime string
            'inclusive' => true,
        ]
    ];
    

    within your callback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常