admin管理员组文章数量:1125027
With WP_Query, I want to retrieve all the published posts with ID greater than a particular ID. Is this possible?
I could not find any way. I tried many variations of this, but I don't get the expected result:
apply_filters(
'widget_posts_args',
array(
'posts_per_page' => 20,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ID',
'value' => 296380,
'type' => 'numeric'
'compare' => '>',
),
)
)
)
Of course, I tried the meta_query alone too but this one returns some old posts, NOT posts with ID greater than 296380.
I tried the following too, but they either return the same posts as the previous query or the same posts with some correctly retrieved post:
array(
'meta_query' =>
array(
array(
'key' => 'post_date',
'value' => strtotime( '2024-01-16' ),
'type' => 'numeric',
'compare' => '>'
)
)
)
array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => '2024-01-16',
'before' => '2032-01-01',
'inclusive' => true,
),
),
)
Any help is appreciated.
With WP_Query, I want to retrieve all the published posts with ID greater than a particular ID. Is this possible?
I could not find any way. I tried many variations of this, but I don't get the expected result:
apply_filters(
'widget_posts_args',
array(
'posts_per_page' => 20,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'ID',
'value' => 296380,
'type' => 'numeric'
'compare' => '>',
),
)
)
)
Of course, I tried the meta_query alone too but this one returns some old posts, NOT posts with ID greater than 296380.
I tried the following too, but they either return the same posts as the previous query or the same posts with some correctly retrieved post:
array(
'meta_query' =>
array(
array(
'key' => 'post_date',
'value' => strtotime( '2024-01-16' ),
'type' => 'numeric',
'compare' => '>'
)
)
)
array(
'post_type' => 'post',
'date_query' => array(
array(
'after' => '2024-01-16',
'before' => '2032-01-01',
'inclusive' => true,
),
),
)
Any help is appreciated.
Share Improve this question edited Jan 26, 2024 at 9:20 Life after Guest asked Jan 26, 2024 at 8:18 Life after GuestLife after Guest 1415 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 2If the date query doesn't work because of modified post dates (e.g. if the posts' timeline is no longer linear), here's a posts_where
filter's suggestion (untested):
add_filter( 'posts_where', function( $where, $query ) use ( &$wbdb ) {
$id = $query->get( '_id__gt' );
if( is_int( $id ) && $id > 0 ) {
$where .= $wpdb->prepare(
" AND %i.ID > %d ",
"{$wpdb->posts}",
$id
);
}
return $where;
}, 10, 2 );
to support this type of _id__gt
argument in WP_Query
for ID greater than some positive integer number:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'_id__gt' => 296380, // <-- our new input argument!
);
$my_query = new WP_Query( $args );
But using queries based on some given ID values in production, sounds brittle.
本文标签: wp queryWPQuery to find any published post with ID greater than given ID
版权声明:本文标题:wp query - WP_Query to find any published post with ID greater than given ID 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736651018a1946140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp_postmeta
in default installation", so your first meta query will result in a query which looks likewp_postmeta.meta_key = 'ID' AND CAST(wp_postmeta.meta_value AS SIGNED) > '296380'
and notwp_posts.ID > 296380
. And yes, you can use$wpdb
directly to get the full post data or just the post IDs, but what you're trying to do can be achieved via a singleWP_Query
, just as shown in @birgire's answer, where he used the hookposts_where
to add the custom SQL query. – Sally CJ Commented Jan 26, 2024 at 23:42WP_Query
, which among others, ensures that plugins can filter the query when necessary. But it should be noted that greater ID does not mean the post is new(er), so if your goal is to get the posts posted or modified after the post 296380, then a date query like this would do it -'date_query' => array( 'relation' => 'OR', array( 'after' => '2024-01-16', 'column' => 'post_date', ), array( 'after' => '2024-01-16', 'column' => 'post_modified', ), )
– Sally CJ Commented Jan 27, 2024 at 0:00