How to filter posts modified after specific date in Wordpress API v2
Asked Answered
U

3

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

Untangle answered 1/11, 2017 at 11:7 Comment(0)
D
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!

Dorella answered 7/11, 2017 at 11:33 Comment(5)
Can I add the above add_filter code here github.com/WP-API/rest-filter/blob/master/plugin.php#L18 after fork & using that updated plugin will work?Untangle
@VigneshSundar I wouldn't recommend to modify the core code, instead create a custom plugin. That way you don't loose the modifications each time you update WordPress core.Dorella
But I think rest-filter is not part of WP-API merged with WordPress core, it is a plugin we can add separately & updating WordPress will not affect thisUntangle
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 laterDorella
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'Untangle
B
11

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/

Bettencourt answered 10/4, 2021 at 3:17 Comment(2)
apparently it is not documented yet: developer.wordpress.org/rest-api/reference/postsHellcat
Can confirm that this is now in the documentationSeemaseeming
D
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!

Dorella answered 7/11, 2017 at 11:33 Comment(5)
Can I add the above add_filter code here github.com/WP-API/rest-filter/blob/master/plugin.php#L18 after fork & using that updated plugin will work?Untangle
@VigneshSundar I wouldn't recommend to modify the core code, instead create a custom plugin. That way you don't loose the modifications each time you update WordPress core.Dorella
But I think rest-filter is not part of WP-API merged with WordPress core, it is a plugin we can add separately & updating WordPress will not affect thisUntangle
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 laterDorella
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'Untangle
U
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.

Untangle answered 13/6, 2018 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.