如何在 Wordpress API v2 中过滤特定日期之后修改的帖子

Posted

技术标签:

【中文标题】如何在 Wordpress API v2 中过滤特定日期之后修改的帖子【英文标题】:How to filter posts modified after specific date in Wordpress API v2 【发布时间】:2018-04-13 16:31:04 【问题描述】:

我正在尝试通过 WordPress REST API 2.0-beta15 和 WordPress v4.8.3 获取(过滤)在特定日期之后修改的帖子,并使用我的客户端应用程序中的现有帖子进行更新。

使用 WordPress 提供的 afterbefore 查询参数,我可以根据 date 而不是 modified 字段过滤帖子。

我已经尝试使用 https://github.com/WP-API/rest-filter 提到的 in this issue /wp-json/wp/v2/posts?filter[date_query][0][column]=post_modified_gmt&filter[date_query][0][after]=1+month+ago,但是这个 date_query 过滤器现在也不起作用。

我需要任何选项,例如

http://bankersdaily.in/wp-json/wp/v2/posts?modified_after=2017-10-31T13:32:10&_envelope http://bankersdaily.in/wp-json/wp/v2/posts?after=2017-10-31T13:32:10&field=modified&_envelope

参考资料:

https://developer.wordpress.org/rest-api/reference/posts/#list-posts https://codex.wordpress.org/Class_Reference/WP_Query?#Date_Parameters

【问题讨论】:

【参考方案1】:

原生支持 - WordPress 5.7

从 WordPress 5.7 开始,支持按修改后日期而非发布日期进行查询。不再需要自定义解决方法。

用法:

/wp-json/wp/v2/posts??modified_after=2021-01-01T00:00:00Z

备注:https://make.wordpress.org/core/2021/02/23/rest-api-changes-in-wordpress-5-7/

【讨论】:

显然还没有记录:developer.wordpress.org/rest-api/reference/posts【参考方案2】:

我已经创建了一个WordPress插件WP REST API – Filter posts date wise using given column,有需要的可以使用。

使用这个插件,我们可以指定列(datedate_gmtmodifiedmodified_gmt 中的任何一个)作为查询参数date_query_column 来查询before 中给出的值和/或after查询参数。

用法

date_query_column 参数与before 和/或after 参数结合使用在任何post 端点上,例如/wp/v2/posts/wp/v2/pages

/wp-json/wp/v2/posts??after=2017-11-08T13:07:09&date_query_column=modified

Github Repository 相同。

【讨论】:

【参考方案3】:

好像不支持,略过docs

以下是一些解决方法:

1)自定义modified_after休息查询参数

我们可以为post 帖子类型添加modified_after 休息查询参数:

add_filter( 'rest_post_collection_params', function( $query_params ) 
    $query_params['modified_after'] = [
        'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),
        'type'        => 'string',
        'format'      => 'date-time',
    ];
    return $query_params;
 );

然后相应地修改其余的帖子查询:

add_filter( 'rest_post_query', function( $args, $request ) 
    if( isset( $request['modified_after'] ) && ! isset( $request['after'] ) ) 
        $args['date_query'][0]['after'] = $request['modified_after'];
        $args['date_query'][0]['column'] = 'post_modified';
    
    return $args;
, 10, 2 );

我们让after 优先于modified_after

示例:

/wp-json/wp/v2/posts??modified_after=2017-11-07T00:00:00

注意事项:

我们可能在post_modified_gmt 列中使用了modified_gmt_after

使用比modified_after 更独特的名称可能会更好,以避免将来可能发生名称冲突。

要将其扩展到其他帖子类型,我们可以使用rest_$post_type_collection_paramsrest_$post_type_query 过滤器。

另一种选择是创建自定义端点和参数,这需要做更多的工作。当然,我们是否应该在当前的 rest api 中添加自定义参数是一个问题。在某些情况下应该没问题,因为我们不会删除或修改响应,也不会更改其他参数的工作方式。

2)自定义date_query_columnrest查询参数

另一种方法是引入自定义date_query_column 休息查询参数:

add_filter( 'rest_post_query', function( $args, $request ) 
    if ( ! isset( $request['before'] ) && ! isset( $request['after'] ) )
        return $args;

    if( isset( $request['date_query_column'] ) )
        $args['date_query'][0]['column'] = $request['date_query_column'];

    return $args;
, 10, 2 );

add_filter( 'rest_post_collection_params', function( $query_params ) 
    $query_params['date_query_column'] = [
            'description' => __( 'The date query column.' ),
            'type'        => 'string',
            'enum'        => [ 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', 'comment_date', 'comment_date_gmt' ],
        ];
    return $query_params;
 );

如果设置了afterbefore 参数,这将可用。

示例:

/wp-json/wp/v2/posts??after=2017-11-07T00:00:00&date_query_column=post_modified

希望对你有帮助!

【讨论】:

我可以在 fork 之后在 github.com/WP-API/rest-filter/blob/master/plugin.php#L18 添加上面的 add_filter 代码吗?使用更新的插件可以工作吗? @VigneshSundar 我不建议修改核心代码,而是create a custom plugin。这样您就不会在每次更新 WordPress 核心时丢失修改。 但我认为rest-filter不是WP-API merged with WordPress core的一部分,它是一个我们可以单独添加的插件&更新WordPress不会影响这个 我的方法是不考虑那个插件。出于同样的原因,我也会避免修改 3rd 方插件。我现在在手机上,所以我稍后会检查它 感谢您的指导,我将创建新插件,但仅供参考,刚才我从 rest-filter 描述中看到了几句话 - 'removed from the API when it was merged into WordPress core'

以上是关于如何在 Wordpress API v2 中过滤特定日期之后修改的帖子的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 WordPress REST API v2 搜索帖子?

WordPress REST API v2 返回所有图像,而不仅仅是来自选定的帖子

推特机器人可以发布推文(或引用)mla V2 API 吗? (基本访问)

Wordpress REST API:如何在 WP REST API JSON 文件中获取“纯文字”内容?

如何渲染wordpress api的内容?

如何在子文件夹中为 WordPress REST API 配置 nginx?