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

Tips and Tricks HQ

  • Home
  • Blog
  • Projects
    • All Projects
    • Simple WP Shopping Cart
    • WP Express Checkout Plugin
    • WP Download Monitor
    • WP Security and Firewall Plugin
    • WP eStore Plugin
    • WP Affiliate Platform
    • WP eMember
    • WP Lightbox Ultimate
    • WP Photo Seller
  • 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.

You are here: Home / Wordpress / 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:
    July 5, 2017 at 8:04 am

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

  2. Amit says:
    July 4, 2017 at 11:54 pm

    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?

  3. vivek says:
    August 12, 2015 at 12:02 am

    thank you.. its working. 🙂

  4. john says:
    March 19, 2015 at 3:07 am

    Thanks for this tips and tricks. they worked well. Thank you

  5. admin says:
    July 19, 2013 at 2:37 am

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

  6. RW says:
    July 18, 2013 at 7:36 pm

    How can I add Title to this example?

    post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;

    Thanks,
    Bob

  7. Ansy says:
    May 5, 2013 at 5:45 pm

    After a long search I found this and its working .. Thanks a lot.

  8. Santosh says:
    March 8, 2013 at 1:39 am

    Thanks its very helpfull .. 🙂

  9. Tom Frearson says:
    January 27, 2013 at 5:16 am

    Really helpful guide, especially the part about using the filters.

    Many thanks!

  10. Ramesh singh says:
    December 11, 2012 at 1:43 pm

    Hello. I m fresher in PHP..It helped me so much in fetching single post in wordpress…Thanks a lot!!!

  11. kumar says:
    October 10, 2012 at 11:59 am

    it saved my day . . thnx for this man

  12. Axel Storckenfeldt says:
    February 29, 2012 at 10:44 am

    Thank you! Worked like charm! With some trixing i got the title and meta’s right as well 🙂

  13. Jeyakumar says:
    January 30, 2012 at 2:20 am

    Thanks for post this article .. This article saved my time.Keep posting like this

  14. svein husjord says:
    January 20, 2012 at 3:57 pm

    Hi! Thanks a lot for this very particular and helpful code snippet. It couldn’t be clearer, or more helpful.

  15. admin says:
    January 5, 2012 at 6:22 am


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

  16. Andre says:
    January 4, 2012 at 11:43 am

    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?

  17. admin says:
    December 16, 2011 at 5:56 am

    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?

  18. Damir Calusic says:
    December 16, 2011 at 3:40 am

    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?

  19. Sanam Maharjan says:
    December 7, 2011 at 5:53 am

    Thanks a lot. Worked as a charm 🙂

  20. Edgars says:
    December 4, 2011 at 8:24 pm

    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.

  21. Adam says:
    December 1, 2011 at 4:55 am

    thanks for sharing 🙂

  22. avinash gaud says:
    November 22, 2011 at 4:55 am

    Hey thanks for sharing the tip

  23. admin says:
    October 12, 2011 at 11:54 pm

    @Robin, The example I added under the “Query X Number of Recent Posts” section of this post might help in your case.

  24. Robin says:
    October 12, 2011 at 8:00 am

    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!

  25. admin says:
    September 21, 2011 at 7:47 pm

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

  26. sonofara says:
    September 21, 2011 at 6:59 am

    very useful, but is there anyway to get post meta (custom fields)?

  27. web design says:
    September 13, 2011 at 7:32 am

    good stuff, used one of your codes with an &offset, now I’m flying

  28. Unloari says:
    September 6, 2011 at 1:34 am

    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.

  29. Auz says:
    August 16, 2011 at 8:15 pm

    Thanks for the snippet!

  30. Avenart says:
    June 15, 2011 at 5:15 am

    Very helpful…Specially for displaying few specific posts. Thank you!

  31. Shaoib Ahmed says:
    June 7, 2011 at 6:14 am

    thnaks alot buddy 🙂 u rock

  32. Bogdan Dimitrov says:
    May 28, 2011 at 7:38 am

    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!

« Older 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

wordpress estore plugin
wordpress membership plugin
WP Express Checkout Plugin
WordPress Lightbox Ultimate Plugin
WordPress photo seller plugin
wordpress affiliate plugin

Recent Posts

  • 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 [...]
  • PayPal QR Codes [...]

Comment & Socialize

  • @Rodrigo Souza, Thank you f ...
    - admin
  • The example for 'slm_add_ed ...
    - Rodrigo Souza
  • @Ron, All the valid transac ...
    - admin
  • Hello, when people have sel ...
    - Ron
  • We have hte following featu ...
    - 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 © 2023 | Tips and Tricks HQ