Get Post Title Outside “The Loop”

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";);

NOTE: As Milan has pointed out, it is not the best method for this, this is the suggested method instead:

$postdata = get_post($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