10

I am trying to fetch(filter) the posts modified after specific date through WordPress REST API 2.0-beta15 & WordPress v4.8.3 and update that with the existing posts in my client app.

Using after and before query params provided by WordPress I can filter posts based on date instead of modified field.

I have tried /wp-json/wp/v2/posts?filter[date_query][0][column]=post_modified_gmt&filter[date_query][0][after]=1+month+ago using this https://github.com/WP-API/rest-filter as mentioned in this issue, but this date_query filter is also not working now.

I need any option like

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

References:

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

Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38

3 Answers3

10

It looks like it's not supported, skimming through the docs

Here are some workarounds:

1) Custom modified_after rest query parameter

We can add the modified_after rest query parameter for the post post type with:

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;
} );

and then modify the rest post query accordingly with:

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

where we let after take priority over modified_after.

Example:

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

Notes:

We might have used modified_gmt_after for the post_modified_gmt column.

It might be better to use a more unique name than modified_after to avoid a possible future name collision.

To extend this to other post types, we can use the rest_{$post_type}_collection_params and the rest_{$post_type}_query filters.

Another option is to create a custom endpoint and parameters, that's a more work to do there. It's of course a question if we should add a custom parameter to the current rest api. In some cases it should be ok, as we're not removing or modifying the response, or changing how other parameters work.

2) Custom date_query_column rest query parameter

Another approach would be to introduce a custom date_query_column rest query parameter:

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;
} );

that would be available if either after or before parameters are set.

Example:

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

Hope it helps!

birgire
  • 11,258
  • 1
  • 31
  • 54
  • Can I add the above `add_filter` code here https://github.com/WP-API/rest-filter/blob/master/plugin.php#L18 after fork & using that updated plugin will work? – Vignesh Sundaramoorthy Nov 07 '17 at 14:13
  • @VigneshSundar I wouldn't recommend to modify the core code, instead [create a custom plugin](https://codex.wordpress.org/Writing_a_Plugin). That way you don't loose the modifications each time you update WordPress core. – birgire Nov 07 '17 at 14:17
  • But I think [rest-filter](https://github.com/WP-API/rest-filter) is not part of [WP-API merged with WordPress core](https://github.com/WP-API/WP-API), it is a plugin we can add separately & updating WordPress will not affect this – Vignesh Sundaramoorthy Nov 07 '17 at 14:27
  • My approach was without that plugin in mind. I would also avoid modify 3rd party plugins for the same reason. Im on mobile now so I will check it later – birgire Nov 07 '17 at 14:39
  • Thanks for the guidance, I will create new plugin, But FYI few words from rest-filter description just now I was seen - '[removed from the API when it was merged into WordPress core](https://github.com/WP-API/rest-filter/blob/master/plugin.php#L4)' – Vignesh Sundaramoorthy Nov 07 '17 at 14:55
10

Native Support - WordPress 5.7

As of WordPress 5.7, support has been added for querying by the post modified date rather than the published date. Custom workarounds are no longer required.

Usage:

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

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

Craig Wayne
  • 4,499
  • 4
  • 35
  • 50
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58
0

I have created a WordPress plugin WP REST API – Filter posts date wise using given column, those who need can use this.

Using this plugin we can specify the column(any of date, date_gmt, modified, modified_gmt) as query parameter date_query_column to query against value(s) given in before and/or after query parameters.

Usage

Use the date_query_column parameter on any post endpoint such as /wp/v2/posts or /wp/v2/pages in combination with before and/or after parameter.

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

Github Repository of the same.

Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38