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.

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

Found this article interesting? Subscribe to Tips and Tricks HQ

email icon rss feed icon twitter icon google plus icon

Comments (87 responses)

  1. enam ahmed says:

    Very helpful thank you very much……

  2. Steve says:

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

  3. admin says:

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

    $publish_date = $queried_post->post_date;

  4. John says:

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

  5. Nick says:

    Perfect. Thanks so much!

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

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

  8. admin says:

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

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

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

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

  12. Thankx friend very usefull code… Great Work

  13. admin says:

    Yeah, just use the following:

    $excerpt = $queried_post->post_excerpt;

  14. 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.”
    +1

  15. Bowe says:

    Thanks this is great!

  16. kemer emlak says:

    thanks a lot, great information

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

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

  19. valentina says:

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

  20. admin says:

    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

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

  22. admin says:

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

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

  24. Josh L says:

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

  25. tim says:

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

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

  27. Arun says:

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

  28. JP says:

    can I show the Last Post ? tnx

Speak Your Mind

*