WordPress.org

Ready to get started?Download WordPress

Forums

getting post ID from a plugin (again...) (2 posts)

  1. SelinaK
    Member
    Posted 2 years ago #

    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 :(

  2. Glenn Ansley
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic