wp query - WP_Query for showing specific post by id

admin2025-06-03  2

but it just shows the first post, I need to show three posts, please help me

$the_query = new WP_Query(array(

 'p' => '272 282 292',)); 

if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();?>

  // Do Stuff
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();

but it just shows the first post, I need to show three posts, please help me

$the_query = new WP_Query(array(

 'p' => '272 282 292',)); 

if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();?>

  // Do Stuff
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Share Improve this question asked Jan 30, 2019 at 21:45 Ehsan SiahtiriEhsan Siahtiri 211 gold badge1 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 12

For multiple post IDs, in WP_Query arguments, we can use:

'post__in' => [ 272, 282, 292 ],

but for a single post ID it's:

'p' => 123,

Note that the default post type is post.

Checking out the SQL query

It's informative to checkout the generated SQL query of WP_Query.

Try e.g. in the wp-cli shell:

wp> $q = new WP_Query( [ 'post__in' => [ 272, 282, 292 ] ] );
wp> echo $q->request;

That way you can make sure it's generating what you need.

Compare e.g. the post__in and the p case.

Helpful docs

It's also helpful to look at the Developer's Handbook:

https://developer.wordpress/reference/classes/wp_query/

and in some cases the old Codex:

https://codex.wordpress/Class_Reference/WP_Query

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748959601a315168.html

最新回复(0)