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.
Find out if we are in a particular WordPress post
Lets say you want to apply some custom tweak when a particular post is being viewed. You will need to programmatically determine when you are in this specific post (example: Post ID 2). The following snippet of code will be helpful for this:
if (is_single("2"))
{
//Do your custom tweak for post whose ID is 2
}
You can do the same thing for pages too (5 is the page ID in this example):
if (is_page("5"))
{
//Do your custom tweak for post whose ID is 2
}
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.
Getting the URL of a post/page
The post/page object that you retrieve using the get_post() function doesn’t actually have the URL of the post. It does have a guid file which contains the URL but that one is not reliable. Use the following function to get the URL value of the post/page:
$post_id = '26'; $post_url = get_permalink($post_id);
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?
😀 it was exxerpt. It’s my mistake!
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!
You probably should to echo “$excerpt” instead of “$content”?
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!
Amazing life saver.. Thanks a lot.
To display in side bar I used PHP Code widget
Thanks again
@Sakura, what do you mean by another post table? Your WordPress posts are saved in wp_posts table by default.
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.
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
@Naveed, try the following:
$queried_post->guid
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
very nice hack now i resolved my problem, thanks!
Thanks for this great explanation and code!
Very nice article, just what I needed.
Thanks !
Very neat trick. Some of these tips I just never would have even thought of
Thanks for nice post ,very very thanks
Thanks for posting this! I spent about an hour looking through the poorly explained wordpress codex and despairing before I found this. Brilliant thanks!
Very Good Tutorial I use
Thanks for this great tricks.
Mehedi Hasan
Web Designer & Developer
When you retrieve the post you should be able to get everything associated with that post (this should include custom fields too)
does this also work with the custom fields of the post? bcz the wordpress documentations doesnt seem to include this
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.
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!
Very helpful thank you very much……
Very useful – thank you very much. Much better than having to use yet another plugin 🙂
@John, yep, simply use the following after you query the post:
$publish_date = $queried_post->post_date;
hey everyone, thanks for this
any idea how I can display the time a certain post was published? thanks a lot.
Perfect. Thanks so much!
@Nick, when you retrieve the post content from database you get the unfiltered content so you need to apply filters to it. I have added an example in this post to demonstrate how it can be done… checkout the “How to Display the Post Content Like WordPress” section of this post.
Sorry, I should have been more clear. WordPress automatically inserts beginning and ending tags when you are using the editor.
In the case of this one post that you have helped me output with your code, it is not inserting those tags. The result is that I get one big paragraph from all the text.
Let me know if I can add more clarification on my problem.
@Nick, what do you mean by auto generated tags?
This is what I was looking for, but I’m having an output problem.
The post displays, but it strips all the tags that are auto-generated by WordPress. Do you know why that would be?
Thanks for this post!
Once you have retrieved the specific post you can write your HTML code that links to the post and use the title as the anchor text.
Hi there
This is great thanks so much
I Was wondering if there is a way to show the “title” and “post thumbnail” only. With a link to the queried post?
Thankx friend very usefull code… Great Work
Yeah, just use the following:
$excerpt = $queried_post->post_excerpt;
“does anyone know how to change this code to just show an excerpt instead? i’d really appreciate any help as i‘m stuck.”
+1
Thanks this is great!
thanks a lot, great information
Needed to give my client access to edit one custom div area on the home page. A theme option or widget wasnt ideal, shed need to learn html to style it. Your code worked perfectly!!! So I created a page for her, hid it from the menu and grabbed the content with your code. Now she can style away 🙂 Just the solution I was looking for. Thank you so much for showing me how!
does anyone know how to change this code to just show an excerpt instead? i’d really appreciate any help as i‘m stuck.
Took out the ‘cat=22&’ and it works. thank you.
Try something like this:
$current_month = date(‘m’);
$current_year = date(‘Y’);
query_posts(“cat=22&year=$current_year&monthnum=$current_month&order=ASC”);
// The wordpress loop goes here
Hi, this is great. But could you tell me how I would go about displaying all posts only for the current month please? And where should I put the code? Thanks.
get_post() is a wordpress function so you will need to access this from a template file.
Hi, trying to make this work in combination with a popup php using “shadowbox js”-plug-in. Getting this error: Fatal error: Call to undefined function get_post() in …….
So kind of a lightbox effect with the content (table) of a specific post or page ID.
Any tips or clues? Help is greatly appreciated
Thanks! I’ve also been looking around for this.
Thank you! Just what i needed. I have been searching far and wide for this.
I am not exactly sure what you are after but yes you can show the last post. You can basically show any post you want. You just need to know the post id/number of the post that you are trying to show.
for example, if the post id of your your last post is 450 then use example 1 from above and replace 26 with 450. This will show that post.
Thanks for this helpful post. I was looking for this type of information and finally found it.
can I show the Last Post ? tnx