简体   繁体   中英

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. By using following code I'm getting data by post_date rather then post_modified . I have post by 25-12-2017 modified date and its post date was 23-12-2017. 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.

Here is a sample code:

Write this in your functions.php file

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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