Support » Plugins and Hacks » Hacks » getting post ID from a plugin (again…)

  • I’m writing a plug-in and need to get the post ID. I stripped the plug-in code down to this and it’s not working.

    <?php
    /*
    Plugin Name: Post-Comment Limit
    */
    
    function lpu_show_id() {
      global $post;
      $postid = $post->ID;
      return $postid;
    }
    
    $id = lpu_show_id();
    echo "Post: ".$id;
    
    ?>

    I tried $wp_query, not working.

    function lpu_show_id() {
     global $wp_query;
     $thePostID = $wp_query->post->ID;
     return $thePostID;
    }

    I presume I’m missing something easy. Help, please 🙁

Viewing 1 replies (of 1 total)
  • That code is being called before WordPress sets up any post data. You’re code snippets are out of context and I don’t think I can give you any advice based on that specific code.

    You need to hook it to an action that takes place at or after template_redirect probably.

Viewing 1 replies (of 1 total)
  • The topic ‘getting post ID from a plugin (again…)’ is closed to new replies.