I am using the code below to generate posts created in the last 48 hours.
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
But I also want to get the posts modified in the last 48 hours. Even if their creation date is older. Does anyone know how to do this?
I'd appreciate your help...
I am using the code below to generate posts created in the last 48 hours.
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
But I also want to get the posts modified in the last 48 hours. Even if their creation date is older. Does anyone know how to do this?
I'd appreciate your help...
Share Improve this question edited Jan 25, 2016 at 7:38 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Jan 25, 2016 at 3:20 cody.mecody.me 11 bronze badge 1- codex.wordpress/Function_Reference/the_modified_date ? – mariocatch Commented Jan 25, 2016 at 4:14
1 Answer
Reset to default 2You can do it easily with WP_Query
:
$args = array(
'date_query' => array(
'relation' => 'OR',
array(
'column' => 'post_date',
'after' => '-2 days'
),
array(
'column' => 'post_modified',
'after' => '-2 days'
)
)
);
$query = new WP_Query( $args );
More information about date_query
parameter of WP_Query
.
If, for some reason, you still want to alter the WHERE
clausele, something like this should work (not tested):
function filter_where($where = '') {
$where .= " AND ( post_date > '" . date('Y-m-d', strtotime('-2 days')) . " OR post_modified > " . date('Y-m-d', strtotime('-2 days')) . " )'";
return $where;
}
add_filter('posts_where', 'filter_where');
Also, you must note that in the post where filter, you are using Y-m-d
date format, which doesn't include time. That can cause unexpected results; you may need to use MySQL datetime format: Y-m-d H:i:s
.