Support » Fixing WordPress » Getting current Post ID ? (out of The Loop)

  • Hi again,

    I’ve been using a similar function to that used by the Blix theme (BX_get_recent_posts) which lists the most recent posts in the blog. Problem is, like the Blix function it relies on the post id ‘p=1’ –passed by clicking on the post title when in Non-Permalinks mode– to hide the current active Post from the list.

    Anyone know of a sure method for getting the current post id to a function in the sidebar?

    Thanks in advance 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • It will work just fine with mod_rewrite permalinks, all mod_rewrite does is map the fancy permalinks to query strings.


    $post = $wp_query->post;

    Then pass the function:


    masquerade, with custom permalinks, unless using %post_id% as part of your structure you don’t have the ID available through the GET query (just the post name through ‘?name=name-of-post’).

    Thats neat Kaf 😉 thanks a bunch. I’ll try it out and if all is well mark this as resolved.

    Oh and masquerade thanks for your assistance, but like Kaf has said activating permalinks changes the entire structure which is why I posted this question :).

    Just for anyone else reading this, keep in mind this is a small issue with the Blix theme. Although not fatal, the recent posts generation partly relies on non-permalink structure to deduce the current Post.

    As expected it works. Thanks again.

    Actually Kaf, can you explian to me where $post = $wp_query->post; comes from?

    The post data is already sort of, to use a technical term, floating around as a query object on a single post page. Assigning $post the current post arrary through $wp_query is but one way to access it.

    As the scope of $post in a page is global, another that works in many (though not all) cases is to use $post without “claiming” it first. When it doesn’t, you could try setting it to global:

    global $post;
    echo $post->ID;

    Also note this single line of the previous code works just as well:


    Though it reads a bit geek.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Getting current Post ID ? (out of The Loop)’ is closed to new replies.