Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query post using post id

Tags:

wordpress

Someone can tell me what's the best way to get a post using its id?

I am using this:

$query = query_posts('post_id='.$_GET['php_post_id']);
global $post;           
foreach ($query as $post):

do stuff...

<?php endforeach;
 wp_reset_query(); ?>

This is returning an array with all post

like image 319
Salvatore Dibenedetto Avatar asked Dec 20 '25 11:12

Salvatore Dibenedetto


2 Answers

get_post( $post_id, $output );

So in practice will look like:

$my_id = 7;
$post_id_7 = get_post($my_id);

Further reference about the post's parameters and fields, here: http://codex.wordpress.org/Function_Reference/get_post

Update: It's the best practice when you need to get a single post by id, no cicles required.

Change post_id= to p=.

$setQuery = 'p='.$_GET['php_post_id'];
query_posts($setQuery);

Click in this link to see: Retrieve a Particular Post

like image 22
Mr Genesis Avatar answered Dec 22 '25 01:12

Mr Genesis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!