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

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.

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 if ( $counter



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:

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:

‘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 You can also use the Get-a-Post WordPress plugin to query a specific post.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s