wp query - Get only modified posts

I'm using this code for generating a Feed from lasted modified postmysqli_query( $conn, "SELECT * FROM wp_post

I'm using this code for generating a Feed from lasted modified post

mysqli_query( $conn, 
    "SELECT * FROM wp_posts 
     WHERE post_status = 'publish' 
         AND post_type = 'post' 
         AND DATE(post_modified) > DATE(post_date) 
     ORDER BY post_modified DESC 
     LIMIT 50"
);

and it works perfect, now I need to reproduce in a WordPress plugin and I use this code:

 $lastupdated_args = array(
    'paged'               => $paged,
    'orderby'             => 'modified',
    'ignore_sticky_posts' => '1'
);

but in this case it shows all posts ordered my latest modification and not only modified post.

Is it possible to fix?

I'm using this code for generating a Feed from lasted modified post

mysqli_query( $conn, 
    "SELECT * FROM wp_posts 
     WHERE post_status = 'publish' 
         AND post_type = 'post' 
         AND DATE(post_modified) > DATE(post_date) 
     ORDER BY post_modified DESC 
     LIMIT 50"
);

and it works perfect, now I need to reproduce in a WordPress plugin and I use this code:

 $lastupdated_args = array(
    'paged'               => $paged,
    'orderby'             => 'modified',
    'ignore_sticky_posts' => '1'
);

but in this case it shows all posts ordered my latest modification and not only modified post.

Is it possible to fix?

Share Improve this question edited Nov 11, 2015 at 18:16 birgire 68.1k7 gold badges120 silver badges252 bronze badges asked Nov 11, 2015 at 14:26 user83403user83403
Add a comment  | 

3 Answers 3

Reset to default 2

You can use the posts_where filter:

// Add custom filter
add_filter( 'posts_where', 'wpse_modified' );

// Fetch posts
$query = new WP_Query( $lastupdated_args );

where you can define the filter callback as:

function wpse_modified( $where )
{
   global $wpdb;
   // Run only once:
   remove_filter( current_filter(), __FUNCTION__ );
   // Append custom SQL
   return $where . " AND {$wpdb->posts}.post_modified} != {$wpdb->posts}.post_modified} ';
}

Though it would be handy to be able to use this kind of date queries:

$args = [
    'date_query' => [
        [ 'before' => 'post_modified' ]
    ],
];

and

$args = [
    'date_query' => [
        [ 
            'column'    => 'post_modified_gmt', 
            'after'     => 'post_date_gmt', 
            'inclusive' => false 
        ]
    ],
];

That's maybe an idea for a core ticket! ;-)

$lastupdated_args = array(
    'paged' => $paged,
    'post_status' => 'publish',
    'post_type' = 'post',
    'orderby' => 'modified',
    'ignore_sticky_posts' => '1'
);

I think this will help you.

Maybe something like this will help, i had a situation where i needed to get posts that were modified at least 1 day after they were created:

function dw_get_modified_posts($author, $post_type = 'post') {
    global $wpdb;

    $result = $wpdb->get_results(
        $wpdb->prepare("
            SELECT ID
            FROM `$wpdb->posts`
            WHERE UNIX_TIMESTAMP(post_modified_gmt) - UNIX_TIMESTAMP(post_date_gmt) > 86400
            AND post_type = %s
            AND post_author = %d
        ", $post_type, $author)
    );

    return array_map(function ($post) {
        return $post->ID;

    }, (array) $result);
}

86400 is the 1 day margin which you can edit it, now you can make a wp_query out of it:

$posts_list = new WP_Query([
   'post__in' => dw_get_modified_posts() ?: [0]
]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745147968a4613733.html

相关推荐

  • wp query - Get only modified posts

    I'm using this code for generating a Feed from lasted modified postmysqli_query( $conn, "SELECT * FROM wp_post

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信