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
  • It is possible with eg posts_where SQL filter, but what about using a built-in date query filtering instead in WP_Query? – birgire Commented Jan 26, 2024 at 8:51
  • @birgire It can be done with date too (I have not tried), but why it doesn't work with ID? In SQL this would be quite simple – Life after Guest Commented Jan 26, 2024 at 8:53
  • Why mess with sql strings if WP_Query supports date filtering via input arguments :-) – birgire Commented Jan 26, 2024 at 9:00
  • 1 @LifeafterGuest Note that by "meta query", it means "search in the post meta table, i.e. wp_postmeta in default installation", so your first meta query will result in a query which looks like wp_postmeta.meta_key = 'ID' AND CAST(wp_postmeta.meta_value AS SIGNED) > '296380' and not wp_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 single WP_Query, just as shown in @birgire's answer, where he used the hook posts_where to add the custom SQL query. – Sally CJ Commented Jan 26, 2024 at 23:42
  • 1 So you should do the same, i.e. use WP_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
 |  Show 5 more comments

1 Answer 1

Reset to default 2

If 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