简体   繁体   English

WordPress按修改日期列出帖子

[英]Wordpress list posts by modified date

I did try the following code for get all wordpress post from modified date but I'm not able to get post from modified date. 我确实尝试使用以下代码从修改日期获取所有wordpress帖子,但无法从修改日期获取帖子。 By using following code I'm getting data by post_date rather then post_modified . 通过使用以下代码,我将通过post_date而不是post_modified获取数据。 I have post by 25-12-2017 modified date and its post date was 23-12-2017. 我已在25-12-2017 modified date之前发布了25-12-2017 ,其post date25-12-2017 modified date post date I want get data by post modified date. 我想按修改后的日期获取数据。

Thank you 谢谢

first code: 第一个代码:

<?php
$args = array(
    'posts_per_page' => $Limit,
    'post_type' => 'post',
    'orderby' => 'modified',
    'offset' => ($Page - 1) * $Limit,
    'year'=>date('Y'),
    'monthnum'=>date('m'),
    'day'=> 25,
    'order'=> 'DESC',
);
?>

Second code: 第二个代码:

<?php
$args = array(
    'posts_per_page' => $Limit,
    'post_type' => 'post',
    'orderby' => 'modified',
    'offset' => ($Page - 1) * $Limit,
    'date_query' => array(
        array(
            'after'     => "December 24, 2017",            
        ),
    ),
);
?>

I don't think by default WordPress have this feature, but you can achieve this by posts_where filter. 我不认为默认情况下WordPress具有此功能,但是您可以通过posts_where过滤器实现此功能。

Here is a sample code: 这是一个示例代码:

Write this in your functions.php file 将此写入您的functions.php文件中

function scopePostModifiedDate($sql)
{
    global $wpdb;
    $sql .= $wpdb->prepare(" AND DATE($wpdb->posts.post_modified) >= %s ", '2017-12-25');
    return $sql;
}

And this code whenever you need: 此代码在您需要时:

add_filter('posts_where', 'scopePostModifiedDate');

$args = array(
    'posts_per_page' => $Limit,
    'post_type' => 'post',
    'orderby' => 'modified',
    'offset' => ($Page - 1) * $Limit,
//    'date_query' => array(
//        array(
//            'after'     => "December 24, 2017",            
//        ),
//    ),
);
$custom_query = new WP_Query($args);
remove_filter('posts_where', 'scopePostModifiedDate');
//print_r($custom_query->request); //print SQL query

Refrence: Refrence:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM