Reputation: 1887
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
Upvotes: 10
Views: 13431
Reputation: 19308
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/
Upvotes: 11
Reputation: 1887
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.
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.
Upvotes: 0
Reputation: 11378
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!
Upvotes: 10