最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - Posts modified in the last 48 hours

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

You 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.

发布评论

评论列表(0)

  1. 暂无评论