最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_Query for showing specific post by id

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far