Getting Post Information Outside the WordPress Loop

I know there are functions to like is_single() that will return data about the page, but I’m looking for a way to get the following information outside of the loop:

The category of the single post.

Read More

AND

The title of the single post.

All I would really need is the post ID in question and I could get all the other information. I’ve looked through the functions reference in the codex, but I haven’t found anything. Is this impossible because the script doesn’t even get that information ’til the Loop runs?

(I would need this information in both the header and footer, so before and after the PHP script for the loop, if that is a problem.)

Hopefully someone can offer some insight.

EDIT: To clarify: I want the information to be from the post that is loaded in the loop on the “single” page. (AKA the post they are viewing.) So how would I get this ID in the first place? Basically, when viewing a post, I want to get its category or title, but not while the loop is going.

Related posts

Leave a Reply

7 comments

  1. This is actually quite simple.

    // Gets an array of post objects.  
    // If this is the single post page (single.php template), this should be an
    // array of length 1.
    $posts = get_posts();
    // The post object contains most/all of the data that is accessible through the 
    // normal functions like `the_ID()`, `the_author()`, etc
    var_dump($posts[0]->ID);
    

    This can be performed outside of and it doesn’t affect the normal loop. For example, my single.php template looks like this (line numbers included):

    1. <?php
    2. get_header(); 
    3. $posts = get_posts();
    4. var_dump($posts[0]->ID);
    5. ?>
    6. <div id="post">
    
  2. You could execute your own query and return a $post object and then access it’s attributes through something like

    echo $post->title;
    

    Read about $wpdb and Custom Queries on the Codex. Basically you could run a SQL query using the ID as a where filter through its $wpdb object.

    Another option, more WP-like, would be to use a Custom Query Post, where you could define another loop, returning only your post using its id:

    query_posts('p=2010'); // 2010 being the post's ID  
    

    Then you could run the alternative loop and use the similar $post object to display some information.

  3. This is what I use inside the loop to run an new query; I just tried it outside the loop and it works, on a single.php template page. This will give the title of the latest post in mycategory. I don’t know how to pull the category ID, though.

    $my_query = new WP_Query('category_name=mycategory&showposts=1');
    while($my_query->have_posts()):
        $my_query->the_post();
        the_title();
    endwhile;
    
  4. After the query has run, try running:

    global $wp_query;
    var_dump( $wp_query );
    

    The information you are looking for is probably in $wp_query->query_vars or $wp_query->post

  5. You can use the $wp_query global object, and address all returned posts like this

    $wp_query->posts ;  // contains an array of posts.
    

    Then, if for instance you need to know if the first post is of a certain post_type, you can do this:

    $first_post_type = $wp_query->posts[0]->post_type;
    
  6. Couldn’t you store the $post information in a separate variable on the page, then just echo the data you need out later when you need it (ie, outside the loop)?

    This is predicated on needing the data after the initial loop has run.