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(']]>', ']]>', $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 Query a Post
Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
ReplyDeletebest java training in coimbatore
php training in coimbatore
best php training institutes in coimbatore
Thanks for Sharing an Information to us . If Someone wants to know about Digital Marketing Course and Web Development Courses. I think this is the right place for you.
ReplyDeleteSEO Courses in coimbatore and Digital Marketing Courses in Coimbatore
Thanks for Sharing an Information to us . If Someone wants to know about Digital Marketing Course and Web Development Courses. I think this is the right place for you.
ReplyDeleteSEO Courses in coimbatore and Digital Marketing Courses in Coimbatore