get_the_* problems outside the loop (8 posts)

  1. jkoontz
    Posted 10 years ago #

    I'm working on a plugin and am having problems with the get_the_* functions. It seems to be hit and miss about what works and how to use them.

    For instance, the get_the_title() only works if I pass it the $post_ID, even though the documentation says it's a parameterless function.

    Now, get_the_guid() doesn't work with or without the $post_ID. However, get_permalink($post_ID) works, but not without the $post_ID. But this isn't a function in template-functions-post.php.

    It seems like I'm picking and choosing fucntions from all over and each has a slightly different way of using them. And some are not documented. Is there a better way of grabbing the following information?

    post title
    post content
    post date
    post excerpt
    post permalink

    Again, this is outside the loop. It's a plugin that grabs this information when a post is published and sends it to another script via HTTP.

    Thanks a million.

  2. jkoontz
    Posted 10 years ago #

    Anyone? Pretty please?

  3. Kafkaesqui

    Posted 10 years ago #

    Check my latest reply to your other forum thread:


    I think get_postdata() is a good way around your problems. Here's the post data you can collect (RE: example of $post['SOMETHING'] provided):


    To return the permalink for a post, just use:


  4. jkoontz
    Posted 10 years ago #

    Thanks Kafkaesqui. I've actually been playing around with that method (got the idea from another plugin). I haven't been able to find much about get_postdata(). Is it a new function? Or a very old one?

    It would be nice to use get_excerpt() because, at least what I undestand, the auto-creation of an excerpt if it doesn't already exist.

    Is there a reason why the normal template tags don't work outside the Loop?

  5. Kafkaesqui

    Posted 10 years ago #

    No, get_postdata() is not new. In fact, if one looks at the source for the function they'd find it still uses $tableposts in its SQL query instead of the more proper (since 1.5) $wpdb->posts. Speaking of which (if you want to avoid deprecated functions), you could also try using get_post() -- which is new:

    $post = get_post($post_ID);

    and then assign your variables through the post object's class attributes or variables:

    $post_title = $post->post_title;
    $post_content = $post->post_content;

    and so on.

    The issue with "The Loop"-specific functions is, they need the post object to be cached and available. Run out of the Loop there's no guarantee it will be. Fortunately there are ways to force it.

    EDIT: Thought I'd note that functions like get_postdata() and get_post() are considered internal functions (i.e. not "template tags"), so documentation you might find on them is going to be slight, if at all. Hard enough documenting all the functions users are supposed to know about...

  6. jkoontz
    Posted 10 years ago #

    Thanks again. I got your previous method to work but I think I'll update it to use get_post().

    Just for learning sake, what ways are there for forcing the post object to be cached?

  7. Kafkaesqui

    Posted 10 years ago #

    Two I mention above.

    In many cases one can scope $wp_query to global, then call the post object through $wp_query->post. This is typically used in a function/plugin run off single post queries and the like. Hence my original suggestion in the other thread.

  8. jkoontz
    Posted 10 years ago #

    Hmmm...I wonder why it didn't work. I'll keep playing around with it and post if I find anything. Regardless, the get_post() method will work. Thanks a bunch.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.