【问题标题】:How to filter posts modified after specific date in Wordpress API v2如何在 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

【问题讨论】:

    标签: wordpress wordpress-rest-api wp-api


    【解决方案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/

    【讨论】:

    【解决方案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
      

      希望对你有帮助!

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2020-03-02
      • 1970-01-01
      相关资源
      最近更新 更多