Query or show a specific post in wordpress

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

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.

Tags: , ,

Subscribe to Tips and Tricks HQ to stay informed

email icon rss feed icon twitter icon google plus icon

85 Responses

  • #1 by nele on August 11, 2010 - 11:07 am

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

  • #2 by David on August 2, 2010 - 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.

  • #3 by David Hobson on July 19, 2010 - 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!
    .-= David Hobson´s last blog ..Wedding Invitations =-.

  • #4 by enam ahmed on July 10, 2010 - 3:40 am

    Very helpful thank you very much……

  • #5 by Steve on July 6, 2010 - 5:29 pm

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

  • #6 by admin on June 20, 2010 - 11:54 pm

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

    $publish_date = $queried_post->post_date;

  • #7 by John on June 18, 2010 - 7:01 pm

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

  • #8 by Nick on June 14, 2010 - 5:51 pm

    Perfect. Thanks so much!

  • #9 by admin on June 14, 2010 - 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.

  • #10 by Nick on June 14, 2010 - 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.

  • #11 by admin on June 14, 2010 - 3:35 am

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

  • #12 by Nick on June 13, 2010 - 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!
    .-= Nick´s last blog ..AAMOF, I H8 TMA =-.

  • #13 by admin on May 30, 2010 - 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.

  • #14 by SS on May 30, 2010 - 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?

  • #15 by Mohummad Abdullah on May 6, 2010 - 11:00 am

    Thankx friend very usefull code… Great Work

  • #16 by admin on April 28, 2010 - 9:36 pm

    Yeah, just use the following:

    $excerpt = $queried_post->post_excerpt;

  • #17 by BN on April 28, 2010 - 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

  • #18 by Bowe on April 26, 2010 - 7:07 am

    Thanks this is great!

  • #19 by kemer emlak on March 8, 2010 - 10:26 am

    thanks a lot, great information

  • #20 by Trisha on February 3, 2010 - 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!
    .-= Trisha´s last blog ..BlueDock Theme For WordPress =-.

  • #21 by steve on January 21, 2010 - 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.

  • #22 by valentina on December 15, 2009 - 10:54 am

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

  • #23 by admin on December 15, 2009 - 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

  • #24 by valentina on December 14, 2009 - 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.

  • #25 by admin on November 22, 2009 - 9:14 pm

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

  • #26 by CJ on November 22, 2009 - 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

  • #27 by Josh L on July 10, 2009 - 3:59 am

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

  • #28 by tim on June 22, 2009 - 2:43 am

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

  • #29 by admin on November 20, 2008 - 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.

  • #30 by Arun on November 20, 2008 - 2:57 pm

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

  • #31 by JP on November 20, 2008 - 2:56 pm

    can I show the Last Post ? tnx

Featured & Popular Articles

Tips and Tricks Hot Items

wordpress estore plugin
wordpress membership plugin
WordPress PDF Stamper Plugin
WordPress Lightbox Ultimate Plugin
wordpress affiliate plugin