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);
@Amit, Shortcodes can be used on any WordPress posts/page to display whatever you want to display. Create your shortcode which maybe take the post ID as an argument. The shortcode handle can then output the content however you want to output it.
If I have 3 html content in a single page in WordPress. I want to display each content on separate page. Actualky I want to make a short code for that.so that I can use only short code to display the contents. Then what should I do?
thank you.. its working. 🙂
Thanks for this tips and tricks. they worked well. Thank you
@RW, You can show the title of the post like the following:
$title = $queried_post->post_title;
echo $title;
After you query the post and get the data, you can show it however you like it. Yes you can even do it from a WordPress shortcode too.
How can I add Title to this example?
post_content;
$content = apply_filters(‘the_content’, $content);
$content = str_replace(‘]]>’, ‘]]>’, $content);
echo $content;
Thanks,
Bob
After a long search I found this and its working .. Thanks a lot.
Thanks its very helpfull .. 🙂
Really helpful guide, especially the part about using the filters.
Many thanks!
Hello. I m fresher in PHP..It helped me so much in fetching single post in wordpress…Thanks a lot!!!
it saved my day . . thnx for this man
Thank you! Worked like charm! With some trixing i got the title and meta’s right as well 🙂
Thanks for post this article .. This article saved my time.Keep posting like this
Hi! Thanks a lot for this very particular and helpful code snippet. It couldn’t be clearer, or more helpful.
<?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
?>
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?
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?
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?
Thanks a lot. Worked as a charm 🙂
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.
thanks for sharing 🙂
Hey thanks for sharing the tip
@Robin, The example I added under the “Query X Number of Recent Posts” section of this post might help in your case.
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!
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);
?>
very useful, but is there anyway to get post meta (custom fields)?
good stuff, used one of your codes with an &offset, now I’m flying
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.
Thanks for the snippet!
Very helpful…Specially for displaying few specific posts. Thank you!
thnaks alot buddy 🙂 u rock
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!