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 List all Posts Alphabetically
You can also use the WP Alphabetic Listing WordPress plugin to list all your posts.






#1 by Ansy on May 5, 2013 - 5:45 pm
After a long search I found this and its working .. Thanks a lot.
#2 by Santosh on March 8, 2013 - 1:39 am
Thanks its very helpfull ..
#3 by Tom Frearson on January 27, 2013 - 5:16 am
Really helpful guide, especially the part about using the filters.
Many thanks!
#4 by Ramesh singh on December 11, 2012 - 1:43 pm
Hello. I m fresher in PHP..It helped me so much in fetching single post in wordpress…Thanks a lot!!!
#5 by kumar on October 10, 2012 - 11:59 am
it saved my day . . thnx for this man
#6 by Axel Storckenfeldt on February 29, 2012 - 10:44 am
Thank you! Worked like charm! With some trixing i got the title and meta’s right as well
#7 by Jeyakumar on January 30, 2012 - 2:20 am
Thanks for post this article .. This article saved my time.Keep posting like this
#8 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.
#9 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
?>
#10 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?
#11 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?
#12 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?
#13 by Sanam Maharjan on December 7, 2011 - 5:53 am
Thanks a lot. Worked as a charm
#14 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.
#15 by Adam on December 1, 2011 - 4:55 am
thanks for sharing
#16 by avinash gaud on November 22, 2011 - 4:55 am
Hey thanks for sharing the tip
#17 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.
#18 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!
#19 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);
?>
#20 by sonofara on September 21, 2011 - 6:59 am
very useful, but is there anyway to get post meta (custom fields)?
#21 by web design on September 13, 2011 - 7:32 am
good stuff, used one of your codes with an &offset, now I’m flying
#22 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.
#23 by Auz on August 16, 2011 - 8:15 pm
Thanks for the snippet!
#24 by Avenart on June 15, 2011 - 5:15 am
Very helpful…Specially for displaying few specific posts. Thank you!
#25 by Shaoib Ahmed on June 7, 2011 - 6:14 am
thnaks alot buddy
u rock
#26 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!
#27 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?
#28 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!
#29 by admin on May 26, 2011 - 11:09 pm
You probably should to echo “$excerpt” instead of “$content”?
#30 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!
#31 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
#32 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.
#33 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.
#34 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
#35 by admin on March 17, 2011 - 2:08 am
@Naveed, try the following:
$queried_post->guid
#36 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
#37 by tech on February 20, 2011 - 12:34 am
very nice hack now i resolved my problem, thanks!
#38 by Rico on February 1, 2011 - 11:04 am
Thanks for this great explanation and code!
#39 by Tjen Penge on January 19, 2011 - 10:59 am
Very nice article, just what I needed.
Thanks !
#40 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
#41 by Manish on December 3, 2010 - 3:54 am
Thanks for nice post ,very very thanks
#42 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!
#43 by Avanish Shakya on August 20, 2010 - 1:57 am
Very Good Tutorial I use
#44 by Mehedi on August 19, 2010 - 3:39 am
Thanks for this great tricks.
Mehedi Hasan
Web Designer & Developer
#45 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)