Query or show a specific post in wordpress
Categories: Wordpress
If you are looking for php code or a plugin for your WordPress that takes a post ID and returns the database record for that post then read on. This is very helpful when you want to show a specific post on your homepage or other pages to get more attention. It allows you to design your homepage or a page with the post(s) that you want to be shown on the page rather than the 10 recent posts that the WordPress automatically chooses for you.
PHP Code Example to Query a WordPress Post
Example 1
The following code will Query the post with post id 26 and Show the title and the content.
<?php
$post_id = 26;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
?>
Example 2
The following style could be more useful as it lets the user customise the font easily.
<?php
$post_id = 26;
$queried_post = get_post($post_id);
?>
<h2><?php echo $queried_post->post_title; ?></h2>
<?php echo $queried_post->post_content; ?>
Example 3
Using an Array… The following code will query every post number in ‘thePostIdArray’ and show the title of those posts.
<?php $thePostIdArray = array("28","74", "82", "92"); ?>
<?php $limit = 4 ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); $counter++; ?>
<?php if ( $counter < $limit + 1 ): ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php $post_id = $thePostIdArray[$counter-1]; ?>
<?php $queried_post = get_post($post_id); ?>
<h2><?php echo $queried_post->post_title; ?></h2>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
How to Display the Post Content Like WordPress
When you retrieve the post content from the database you get the unfiltered content. If you want to achieve the same output like WordPress does in its’ posts or pages then you need to apply filter to the content. You can use the following code:
<?php
$post_id = 26;
$queried_post = get_post($post_id);
$content = $queried_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
For a range of all the returned fields that you can use, check the WordPress site here.
Query X Number of Recent Posts
You can use the “wp_get_recent_posts” function to retrieve X number of recent posts and then display them however you want to. Here is an example:
<?php
//Query 5 recent published post in descending order
$args = array( 'numberposts' => '5', 'order' => 'DESC','post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
//Now lets do something with these posts
foreach( $recent_posts as $recent )
{
echo 'Post ID: '.$recent["ID"];
echo 'Post URL: '.get_permalink($recent["ID"]);
echo 'Post Title: '.$recent["post_title"];
//Do whatever else you please with this WordPress post
}
?>Using a Plugin to Query a Post
You can also use the Get-a-Post WordPress plugin to query a specific post.
Articles you may also like:









#1 by Jeyakumar on January 30, 2012 - 2:20 am
Thanks for post this article .. This article saved my time.Keep posting like this
#2 by svein husjord on January 20, 2012 - 3:57 pm
Hi! Thanks a lot for this very particular and helpful code snippet. It couldn’t be clearer, or more helpful.
#3 by admin on January 5, 2012 - 6:22 am
<?php
$key_1_value = get_post_meta('POST-ID', 'Key_1', true); //Use this to retrieve the plain custom field value. Example: 1117,302,1309
$thePostIdArray = explode(",", $key_1_value); //Load the customer field value into an array (each comma will separate an item)
print_r($thePostIdArray); //Lets check the value of this array
?>
#4 by Andre on January 4, 2012 - 11:43 am
For:
$thePostIdArray = array(“28″,”74″, “82″, “92″);
How would I make the post id’s be loaded from a custom field in WordPress?
For example I want to make a custom post type, and a field where a user can enter in order post ID’s.
Like this: 1117,302,1309 – and these will then dynamically be placed in the array?
Is this possible?
The post type, and field is easy, I’m just not sure on how to pass the ID’s from the field into the array.
thoughts?
#5 by admin on December 16, 2011 - 5:56 am
First you would add the image URL as a custom field in that post. Then you can retrieve the value of that custom field and use it to show the image. Are you using a custom field?
#6 by Damir Calusic on December 16, 2011 - 3:40 am
Hi,
I wonder how to show the thumbnail image of the the post with example 2. Everything works but I dont know how to show the image of the post.’
Do you know how to display the image aswell?
#7 by Sanam Maharjan on December 7, 2011 - 5:53 am
Thanks a lot. Worked as a charm
#8 by Edgars on December 4, 2011 - 8:24 pm
Thanks a lot, saved my day when have_posts() for some strange reason started to break.
and to previous comments – for excerpt there is a the_excerpt() function, not post_excerpt.
#9 by Adam on December 1, 2011 - 4:55 am
thanks for sharing
#10 by avinash gaud on November 22, 2011 - 4:55 am
Hey thanks for sharing the tip
#11 by admin on October 12, 2011 - 11:54 pm
@Robin, The example I added under the “Query X Number of Recent Posts” section of this post might help in your case.
#12 by Robin on October 12, 2011 - 8:00 am
I was wondering if you have a method of extracting a range of post id’s into an array to then use and display a range of posts (for example on a homepage). I don’t want to display the range the same way as its for a news site, so I’ve got a feature article, a medium one and a range of small ones. The problem i’ve got is that it keeps starting at the newest post, therefore repeating the newest one 3 times.
This is what I’ve got for a query:
query_posts(‘order=DESC&showposts=1′);
if (have_posts()) : while (have_posts()) : the_post(); ?>
//here i do stuff with the results
<?php endwhile; endif;
}
The show posts is editable for a longer range of course, but out of this I'm wishing for a postID range so i can easily manage it as a single array of posts.
Thanks in advance!
#13 by admin on September 21, 2011 - 7:47 pm
There is a really easy way to get the custom field value of a particular WordPress post. Here is an example:
<?php
$key_1_values = get_post_meta(76, 'key_1');
print_r($key_1_values);
?>
#14 by sonofara on September 21, 2011 - 6:59 am
very useful, but is there anyway to get post meta (custom fields)?
#15 by web design on September 13, 2011 - 7:32 am
good stuff, used one of your codes with an &offset, now I’m flying
#16 by Unloari on September 6, 2011 - 1:34 am
Actually like your websites particulars! Undoubtedly a wonderful offer of information that’s extraordinarily helpful. Carry on to carry publishing and i’m gonna proceed reading by the use of! Cheers.
#17 by Auz on August 16, 2011 - 8:15 pm
Thanks for the snippet!
#18 by Avenart on June 15, 2011 - 5:15 am
Very helpful…Specially for displaying few specific posts. Thank you!
#19 by Shaoib Ahmed on June 7, 2011 - 6:14 am
thnaks alot buddy
u rock
#20 by Bogdan Dimitrov on May 28, 2011 - 7:38 am
O-o I see.
I thinks that this command should work like normal excerpt in loop.
I have another site that I use excerpt in home page loop and it works without adding any thing in excerpt field bellow posts!
Is way to create same functionality in plugin or it is a lot of code?
I’m not programmer! I now little PHP to rewrite some code for my custom templates or themes, but it’s not much. I always create custom theme for WordPress or custom templates for Joomla sites.
So thank You that spend time to answer me!
#21 by admin on May 28, 2011 - 2:14 am
That should work as long as you have entered value in the Post excerpt field of the post. By default the post excerpt is empty. Did you add value in there when you edited the post?
#22 by Bogdan Dimitrov on May 27, 2011 - 10:37 am
Here is code:
//$content = $queried_post->post_content;
//$content = apply_filters(‘the_content’, $content);
//$content = str_replace(‘]]>’, ‘]]>’, $content);
//echo $content;
$excerpt = $queried_post->post_excerpt;
echo $excerpt;
this commented one for CONTENT works well.
But how to make this for EXCERPT work too.
Should I apply some filter, like for content?
Thank You in advice! Have a nice day!
#23 by admin on May 26, 2011 - 11:09 pm
You probably should to echo “$excerpt” instead of “$content”?
#24 by Bogdan Dimitrov on May 26, 2011 - 10:49 am
Thanks for sharing! That is exactly what I need.
I could not show how the show excerpt instead of content.
The code I have currently is:
$excerpt = $queried_post->post_excerpt;
echo $content;
but it doesn’t work.
Could you tell me where is wrong, please?
How to change current code from tutorial?
Thanks again!
#25 by Shovan on April 27, 2011 - 6:37 am
Amazing life saver.. Thanks a lot.
To display in side bar I used PHP Code widget
Thanks again
#26 by admin on April 22, 2011 - 11:20 pm
@Sakura, what do you mean by another post table? Your WordPress posts are saved in wp_posts table by default.
#27 by sakura on April 22, 2011 - 12:06 am
hi, thanks for sharing this tip. I still have a question though,… I want to show all my post on my homepage but I want to get all the post from another post table on the same database. how do I do that? is it even possible?
any response would be appreciated.
thank you.
#28 by stevan on March 23, 2011 - 6:09 pm
Hello, I am Brazilian and I found your blog on something that looked very much, thank you for
tip and I will give to everyone. thanks
#29 by admin on March 17, 2011 - 2:08 am
@Naveed, try the following:
$queried_post->guid
#30 by Naveed Khan on March 16, 2011 - 9:05 pm
Thanks for the share of this post i would like to know how can we get permalink from $queried_post-> ?
Answer will be very thanks full
#31 by tech on February 20, 2011 - 12:34 am
very nice hack now i resolved my problem, thanks!
#32 by Rico on February 1, 2011 - 11:04 am
Thanks for this great explanation and code!
#33 by Tjen Penge on January 19, 2011 - 10:59 am
Very nice article, just what I needed.
Thanks !
#34 by John Gamings on January 3, 2011 - 11:14 am
Very neat trick. Some of these tips I just never would have even thought of
#35 by Manish on December 3, 2010 - 3:54 am
Thanks for nice post ,very very thanks
#36 by Arcadia Ronin on November 14, 2010 - 6:51 pm
Thanks for posting this! I spent about an hour looking through the poorly explained wordpress codex and despairing before I found this. Brilliant thanks!
#37 by Avanish Shakya on August 20, 2010 - 1:57 am
Very Good Tutorial I use
#38 by Mehedi on August 19, 2010 - 3:39 am
Thanks for this great tricks.
Mehedi Hasan
Web Designer & Developer
#39 by admin on August 11, 2010 - 11:48 pm
When you retrieve the post you should be able to get everything associated with that post (this should include custom fields too)
#40 by nele on August 11, 2010 - 11:07 am
does this also work with the custom fields of the post? bcz the wordpress documentations doesnt seem to include this
#41 by David on August 2, 2010 - 8:16 pm
Awesome, thanks for this. First site I went to and found the answer straight away. I thought it was going to be much more complicated than this.
#42 by David Hobson on July 19, 2010 - 1:54 am
Very nice, very helpful! I love just grabbing snippets of code without fully understanding php! Although knowing other languages helps.
Have you considered indenting your code to make it more visibly intuitive? I think it would also be a good example for everyone out there! Thanks boss!
.-= David Hobson´s last blog ..Wedding Invitations =-.
#43 by enam ahmed on July 10, 2010 - 3:40 am
Very helpful thank you very much……
#44 by Steve on July 6, 2010 - 5:29 pm
Very useful – thank you very much. Much better than having to use yet another plugin
#45 by admin on June 20, 2010 - 11:54 pm
@John, yep, simply use the following after you query the post:
$publish_date = $queried_post->post_date;
#46 by John on June 18, 2010 - 7:01 pm
hey everyone, thanks for this
any idea how I can display the time a certain post was published? thanks a lot.
#47 by Nick on June 14, 2010 - 5:51 pm
Perfect. Thanks so much!