How to show specific post(s) in WordPress

Sometimes we need a code for our wordpress plugin to show a specific post from the database. It could be dynamic though, but only 1 specific post.

You can use this code to show a specific post on your wordpress homepage or specific page / widget.

Please note, this is the basic code that you can modify according to your needs.

First goes the code to show only 1 post. Then how to show multiple selected posts:

$post_id = 26;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;

This will echo your selected posts title and the content. You can add html and styling to it as required.

If you want to show more than one posts, you can use array to pass the post IDs like this:

$thePostIdArray = array("1","2", "3");
$limit = 4;
if (have_posts()) {
 while (have_posts()) {
 if ( $counter < $limit + 1 ) { ?>
 <div id="post-<?php the_ID(); ?>">
 $post_id = $thePostIdArray[$counter-1];
 $queried_post = get_post($post_id);
 <h2><?php echo $queried_post->post_title; ?></h2>

If you have any difficulty or queries related to the code leave a message below!

hire best wordpress php css developer
Need Help? Hire me now!
I can help you in anything related to webdevelopment, including: HTML, CSS, PHP & MySql, WordPress (plugins & themes development, others), regular expressions, server management, etc.

Leave a Reply

Your email address will not be published.