Get Post Title Outside "The Loop"
by Lars Koudal on 04/11/2009
Getting individual post data outside the loop can be difficult, but you can quickly get the information you want if you have the id of the post you are trying to get information from:
Use the following code to get the information:
global $wpdb;
$postid='1'; // replace this with the postid you are trying to access.
$postdata= $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$postid");
To access and use the data is equally easy, here are some examples to get you started:
echo $postdata->post_title; //The title of the post
echo $postdata->post_content; //The content of the post
There is other useful information you can grab via this method:
- post_author
- post_date
- post_date_gmt
- post_content
- post_title
- post_category
- post_excerpt
- post_status,
- comment_status
- ping_status
- post_password
- post_name
- to_ping
- pinged
- post_modified
- post_modified_gmt
- post_content_filtered
- post_parent
- guid
- menu_order
- post_type
- post_mime_type
- comment_count

Tagged as: Php Access, Ping, WordPress
Popular Searches
wordpress get post title, get_post_title, wp get post title, get post title, wordpress get title outside loop, post title, function getpost content(), post->title, wordpress not displaying date title, wordpress "get post by title", how to get post title , get post id outside loop, get post by title, get post titles without the loop, $post->title, query posts outside loop, get post title outside loop wordpress, get post id by name, wordpress get content outside loop, get post content outside loop, get post outside of loop, get post title in wordpress, wordpress get post content outside loop, wp get the title, wordpress get post content outside of loop,

Lars Koudal blogs about WordPress development, WordPress news, WordPress Plugins and SEO news in general.
Lars also is also the owner and lead developer of Premium WordPress Plugins available at CleverPlugins.com
{ 8 comments… read them below or add one }
This is actually not the best method for this. Much better is to use WordPress function:
$postdata = get_post($postid);
This function uses internal WordPress object cache, and if the post you need is already in the cache you save on running unneeded query. Object is the same as with your method and all returned properties the same.
.-= Milan Petrovic´s last blog ..New website res4WP.com =-.
Hi Milan
Thank you for that function! That does indeed make it a lot easier, and as you say, if its already in the cache it is much faster. In cases where a lot of posts could be queried it would be good.
One note however, if in cases where you are only interested in for instance the title of a range of posts, would it not be faster to use my original query solution, modified to only return the title from the database, or would get_post() still be better/faster?
Thank you for adding valuable insights to my ramblings!
If the post is already cached it will be faster to use get_post, also get_post will cache post if it is not already. Also, such simple queries are fast do execute. But I try to avoid such coding because it is easier to do, but later is much more complicated to change and clean up.
Since I spend a lot of time optimizing GD Star Rating plugin and adding various caching methods, I have measured all kinds of things, and the conclusion is that the best thing is to have less DB queries. They are very fast, but take into the account total number of queries executed, and it’s best to find the way to minimize that. GD Star Rating pre 1.5 versions needed some 150 queries for 10 posts because it was executing queries as they were needed. Now it needs less then 10 queries for the same 10 posts.
.-= Milan Petrovic´s last blog ..New website res4WP.com =-.
150 down to less than 10? That is what I call improvement!
I am sorry but what is this supposed to do? “outside the loop” of what exactly?
Hi Dave
“The loop” refers to the process WP makes (code-wise) in displaying content to the visitors. In terms of Plugin development, you usually work either outside or inside “the loop”.
If you want to learn more about “the loop”, take a look here: http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/
Thank you! I couldn’t believe the amount of time I had to spend to find such a simple solution.
Was trying to limit the length of the title of the Previous and Next posts (without the need for plugins), since previous_post_link() and next_post_link() are so limited. The following should give anyone else looking for a solution enough of an idea.
$prev_post = get_adjacent_post(false, ”, true);
$prev_post_link = ($prev_post ? get_permalink( $prev_post->ID ) : “”);
$prev_post_title = $prev_post->post_title;
$next_post = get_adjacent_post(false, ”, false);
$next_post_link = ($next_post ? get_permalink( $next_post->ID ) : “”);
$next_post_title = $next_post->post_title;
echo “$prev_post_link$prev_post_title$next_post_link$next_post_title”;
$prev_post_title = substr(($prev_post_title=wordwrap($prev_post_title,25,’\n’)),0,strpos($prev_post_title,’\n’));
$next_post_title = substr(($next_post_title=wordwrap($next_post_title,25,’\n’)),0,strpos($next_post_title,’\n’));
echo “$prev_post_title$next_post_title”;
Hi George
Thanks for dropping by and leaving that piece of code, that is gonna come in handy at some point, I am sure!