How to get a specific post in WordPress
In this tutorial, we will learn for How to get a specific post in WordPress. In most cases, we can get a specific post on the WordPress blog detail page. So in this tutorial is very helpful when you want to show a specific post on your blog details page and other pages to get more.
Simple PHP Code
The below code will Query the post with post id 16 and it will display the title, the content, and the permalink.
<?php
$post_id = 16;
$get_post = get_post($post_id);
$title = $get_post->post_title;
echo $title;
$content = $get_post->post_content;
echo $content;
$content = $get_post->post_content;
$permalink = get_permalink($post_id);
echo $permalink;
?>
Using an Array
The below code will query every post number in PostIds
and display the title of those posts.
<?php
$PostIds = array("16", "23", "45", "79", "97");
$limit = 5;
if (have_posts()) :
while (have_posts()) : the_post();
$count++;
if ( $count < $limit + 1 ): ?>
<div class="post" id="post-<?php the_ID(); ?>">
<?php $post_id = $PostIds[$count-1]; ?>
<?php $get_post = get_post($post_id); ?>
<h2><?php echo $get_post->post_title; ?></h2>
</div>
<?php endif;
endwhile;
endif;
?>