• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Tips and Tricks HQ

  • Home
  • Projects
    • All Projects
    • Simple WP Shopping Cart
    • WP Express Checkout Plugin
    • Accept Stripe Payments
    • WP Download Monitor
    • Easy HTTPS Redirection
    • WP Security and Firewall Plugin
    • WP eStore Plugin
    • WP Affiliate Platform
    • WP eMember
  • Products
    • All Products
    • Checkout
  • Support
    • Support Portal
    • Customer Only Forum
    • WP eStore Documentation
    • WP Affiliate Software Documentation
    • WP eMember Documentation
  • Contact

Query or show a specific post in 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.

Home » Blog » Query or show a specific post in wordpress

Last updated: December 29, 2013





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(']]>', ']]&gt;', $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);

Related Posts

  • Manage Your WordPress Web Ads Easily

Wordpress Code example,  Web Development,  Wordpress,  WordPress tutorial,  WordPress Tweaks

Reader Interactions

Comments (91 responses)

  1. admin says:
    May 28, 2011 at 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?

  2. Bogdan Dimitrov says:
    May 27, 2011 at 10:37 am

    😀 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!

  3. admin says:
    May 26, 2011 at 11:09 pm

    You probably should to echo “$excerpt” instead of “$content”?

  4. Bogdan Dimitrov says:
    May 26, 2011 at 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!

  5. Shovan says:
    April 27, 2011 at 6:37 am

    Amazing life saver.. Thanks a lot.
    To display in side bar I used PHP Code widget

    Thanks again

  6. admin says:
    April 22, 2011 at 11:20 pm

    @Sakura, what do you mean by another post table? Your WordPress posts are saved in wp_posts table by default.

  7. sakura says:
    April 22, 2011 at 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.

  8. stevan says:
    March 23, 2011 at 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

  9. admin says:
    March 17, 2011 at 2:08 am

    @Naveed, try the following:

    $queried_post->guid

  10. Naveed Khan says:
    March 16, 2011 at 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

  11. tech says:
    February 20, 2011 at 12:34 am

    very nice hack now i resolved my problem, thanks!

  12. Rico says:
    February 1, 2011 at 11:04 am

    Thanks for this great explanation and code!

  13. Tjen Penge says:
    January 19, 2011 at 10:59 am

    Very nice article, just what I needed.
    Thanks !

  14. John Gamings says:
    January 3, 2011 at 11:14 am

    Very neat trick. Some of these tips I just never would have even thought of

  15. Manish says:
    December 3, 2010 at 3:54 am

    Thanks for nice post ,very very thanks

  16. Arcadia Ronin says:
    November 14, 2010 at 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!

  17. Avanish Shakya says:
    August 20, 2010 at 1:57 am

    Very Good Tutorial I use

  18. Mehedi says:
    August 19, 2010 at 3:39 am

    Thanks for this great tricks.

    Mehedi Hasan
    Web Designer & Developer

  19. admin says:
    August 11, 2010 at 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)

  20. nele says:
    August 11, 2010 at 11:07 am

    does this also work with the custom fields of the post? bcz the wordpress documentations doesnt seem to include this

  21. David says:
    August 2, 2010 at 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.

  22. David Hobson says:
    July 19, 2010 at 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!

  23. enam ahmed says:
    July 10, 2010 at 3:40 am

    Very helpful thank you very much……

  24. Steve says:
    July 6, 2010 at 5:29 pm

    Very useful – thank you very much. Much better than having to use yet another plugin 🙂

  25. admin says:
    June 20, 2010 at 11:54 pm

    @John, yep, simply use the following after you query the post:

    $publish_date = $queried_post->post_date;

  26. John says:
    June 18, 2010 at 7:01 pm

    hey everyone, thanks for this
    any idea how I can display the time a certain post was published? thanks a lot.

  27. Nick says:
    June 14, 2010 at 5:51 pm

    Perfect. Thanks so much!

  28. admin says:
    June 14, 2010 at 10:50 am

    @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.

  29. Nick says:
    June 14, 2010 at 3:40 am

    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.

  30. admin says:
    June 14, 2010 at 3:35 am

    @Nick, what do you mean by auto generated tags?

  31. Nick says:
    June 13, 2010 at 6:53 pm

    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!

  32. admin says:
    May 30, 2010 at 10:23 pm

    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.

  33. SS says:
    May 30, 2010 at 7:50 pm

    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?

  34. Mohummad Abdullah says:
    May 6, 2010 at 11:00 am

    Thankx friend very usefull code… Great Work

  35. admin says:
    April 28, 2010 at 9:36 pm

    Yeah, just use the following:

    $excerpt = $queried_post->post_excerpt;

  36. BN says:
    April 28, 2010 at 11:38 am

    “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

  37. Bowe says:
    April 26, 2010 at 7:07 am

    Thanks this is great!

  38. kemer emlak says:
    March 8, 2010 at 10:26 am

    thanks a lot, great information

  39. Trisha says:
    February 3, 2010 at 1:38 pm

    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!

  40. steve says:
    January 21, 2010 at 8:35 am

    does anyone know how to change this code to just show an excerpt instead? i’d really appreciate any help as i‘m stuck.

  41. valentina says:
    December 15, 2009 at 10:54 am

    Took out the ‘cat=22&’ and it works. thank you.

  42. admin says:
    December 15, 2009 at 5:58 am

    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

  43. valentina says:
    December 14, 2009 at 10:19 am

    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.

  44. admin says:
    November 22, 2009 at 9:14 pm

    get_post() is a wordpress function so you will need to access this from a template file.

  45. CJ says:
    November 22, 2009 at 7:05 pm

    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

  46. Josh L says:
    July 10, 2009 at 3:59 am

    Thanks! I’ve also been looking around for this.

  47. tim says:
    June 22, 2009 at 2:43 am

    Thank you! Just what i needed. I have been searching far and wide for this.

  48. admin says:
    November 20, 2008 at 9:01 pm

    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.

  49. Arun says:
    November 20, 2008 at 2:57 pm

    Thanks for this helpful post. I was looking for this type of information and finally found it.

  50. JP says:
    November 20, 2008 at 2:56 pm

    can I show the Last Post ? tnx

Newer Comments »

Leave a Reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Featured & Popular Articles

Video Answers to Top WordPress QuestionsWordPress Optimization Tips and Tricks for Better Performance and SpeedEssential WordPress Security Tips - Is Your Blog Protected?WordPress Simple PayPal Shopping Cart PluginTop 15 Search Engine Optimization (SEO) Techniques I Forget to DoList of the Best and Must Use WordPress PluginsHow do I Start a Blog and Make Money Online?Good Domain Name Picking Tips for Your Blog SetupFind Out Which WordPress Web Hosting Company Offers the Cheapest and Reliable Web Hosting Solution

Featured WordPress Plugins

WP Express Checkout Plugin
wordpress estore plugin
wordpress membership plugin
wordpress affiliate plugin

Recent Posts

  • How to Use Browser Developer Tools to Inspect Elements and [...]
  • Accept Donations via PayPal from Your WordPress Site Easil [...]
  • Buy Now Button Graphics for eCommerce Websites [...]
  • Subscription Button Graphics for eCommerce Websites [...]
  • Adding PayPal Payment Buttons to Your WordPress Sidebar Ea [...]

Comment & Socialize

  • @Rob, We have just released ...
    - admin
  • I installed the plugin a co ...
    - Rob
  • @Sebastian, We've released ...
    - admin
  • I've used this plugin on a ...
    - Sebastian Djupsjöbacka
  • @John, this plugin doesn't ...
    - admin

Check out our solutions

View our WordPress plugin collection and start using them on your site.

Our WordPress Solutions

Footer

Company

  • About
  • Privacy Policy
  • Terms and Conditions
  • Affiliate Login

Top WordPress Plugins

  • Simple Shopping Cart
  • PayPal Donations
  • WP Express Checkout
  • WP eStore
  • WP eMember

Blogging Tips

  • How to Start a Blog
  • Selecting a Good Domain
  • Cheap WP Hosting
  • WP Video Tutorials
  • Simple SEO Tips

Search


Keep In Touch

Copyright © 2025 | Tips and Tricks HQ