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

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

$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>
<?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:

$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:

//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);

Comments (91 responses)

  1. admin says:

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

  2. sakura says:

    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.

  3. stevan says:

    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

  4. admin says:

    @Naveed, try the following:


  5. Naveed Khan says:

    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

  6. tech says:

    very nice hack now i resolved my problem, thanks!

  7. Rico says:

    Thanks for this great explanation and code!

  8. Tjen Penge says:

    Very nice article, just what I needed.
    Thanks !

  9. John Gamings says:

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

  10. Manish says:

    Thanks for nice post ,very very thanks

  11. Thanks for posting this! I spent about an hour looking through the poorly explained wordpress codex and despairing before I found this. Brilliant thanks!

  12. Very Good Tutorial I use

  13. Mehedi says:

    Thanks for this great tricks.

    Mehedi Hasan
    Web Designer & Developer

  14. admin says:

    When you retrieve the post you should be able to get everything associated with that post (this should include custom fields too)

  15. nele says:

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

  16. David says:

    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.

  17. David Hobson says:

    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!

  18. enam ahmed says:

    Very helpful thank you very much……

  19. Steve says:

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

  20. admin says:

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

    $publish_date = $queried_post->post_date;

  21. John says:

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

  22. Nick says:

    Perfect. Thanks so much!

  23. admin says:

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

  24. Nick says:

    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.

  25. admin says:

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

  26. Nick says:

    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!

  27. admin says:

    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.

  28. SS says:

    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?

  29. Thankx friend very usefull code… Great Work

  30. admin says:

    Yeah, just use the following:

    $excerpt = $queried_post->post_excerpt;

  31. BN says:

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

  32. Bowe says:

    Thanks this is great!

  33. kemer emlak says:

    thanks a lot, great information

  34. Trisha says:

    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!

  35. steve says:

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

  36. valentina says:

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

  37. admin says:

    Try something like this:

    $current_month = date(‘m’);
    $current_year = date(‘Y’);


    // The wordpress loop goes here

  38. valentina says:

    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.

  39. admin says:

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

  40. CJ says:

    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

  41. Josh L says:

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

  42. tim says:

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

  43. admin says:

    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.

  44. Arun says:

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

  45. JP says:

    can I show the Last Post ? tnx

Speak Your Mind