admin管理员组文章数量:1415476
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 user83403user834033 Answers
Reset to default 2You 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]
]);
本文标签: wp queryGet only modified posts
版权声明:本文标题:wp query - Get only modified posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745147968a2644785.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论