I’m looking for a way to get all posts modified after a specific date, but still couldn’t find an answer for that online; All websites says that this is still not supported until now.
Is querying posts modified after a date supported in WordPress now? If it’s supported, how can I achieve that?
If it’s not supported yet, is there an alternative way I can achieve the same thing by modifying the global $wp_query
object?
It is possible, using the date_query
argument. By default this uses the publication date, but you can tell it to use the modified date by using the column
argument. So if you wanted to query all posts modified after June 30th 2019, you’d use:
'date_query' => [
'column' => 'post_modified',
'after' => [
'year' => 2019,
'month' => 6,
'day' => 30,
],
],
The column
parameter of WP_Date_Query
supports:
post_date
(default)
post_date_gmt
post_modified
post_modified_gmt
comment_date
comment_date_gmt
A WP shell example for the date query in WP_Query
with post_modified_gmt
and the after
parameter:
wp> echo ( new WP_Query( [ 'date_query' => [ [ 'column' => 'post_modified_gmt',
'after' => '2 days ago' ] ] ] ) )->request;
will output:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM tfl_posts WHERE 1=1 AND (
wp_posts.post_modified_gmt > '2019-11-16 10:00:27'
For further information look e.g. into the dev documents here:
https://developer.wordpress.org/reference/classes/wp_date_query/
https://developer.wordpress.org/reference/classes/wp_query/#date-parameters