title: Wordpress get post ID from Widget
body: <p>I am trying to get the post ID outside the loop from a widget, but I get some another page ID instead the current post ID.</p>
  
  <p>My question similar to <a href="https://stackoverflow.com/questions/8957444/wordpress-get-post-id-from-side-widget">wordpress get post id from side widget</a>, but the accepted answer here (add <code>add_action("widgets_init", "your_init");</code>) does not help.</p>
  
  <p>I have tried:</p>
  
  <pre><code>$post_id = get_the_id();
  // or
  $post_id = $GLOBALS['post']-&gt;ID;
  // or
  global $wp_query;
  $post_id = $wp_query-&gt;post-&gt;ID;
  // or
  global $post;
  $post_id = $post-&gt;ID;
  </code></pre>
  
  <p>All return some page ID, not the current post ID.</p>
  
  <p>Here is my post link <a href="http://ori.co.il/roofdagan1/?p=1492" rel="nofollow noreferrer">http://ori.co.il/roofdagan1/?p=1492</a></p>
  
  <p>My widget code:</p>
  
  <pre><code>&lt;?php
      /*
      Plugin Name: custom-post-field
      Description: custom-post-field
      */
  
      // Creating the widget 
      class cpf_widget extends WP_Widget
      {
          function __construct()
          {
              parent::__construct(
              // Base ID of your widget
              'cpf_widget', 
  
              // Widget name will appear in UI
              __('CPF Widget', 'cpf_widget_domain'), 
  
              // Widget description
              array( 'description' =&gt; __( 'Custom Post Field Widget', 'cpf_widget_domain' ), ) 
              );
          }
  
          // Creating widget front-end
          // This is where the action happens
          public function widget( $args, $instance )
          {
              $title = apply_filters( 'widget_title', $instance['title'] );
              // before and after widget arguments are defined by themes
              echo $args['before_widget'];
              if ( ! empty( $title ) )
              echo $args['before_title'] . $title . $args['after_title'];
  
              $post_id = $GLOBALS['post']-&gt;ID;
              $resoult_ = "Post Id : ". $post_id;
              // This is where you run the code and display the output
              echo __( $resoult_, 'cpf_widget_domain' );
              echo $args['after_widget'];
          }
  
          // Widget Backend
          public function form( $instance )
          {
              if ( isset( $instance[ 'title' ] ) )
              {
                  $title = $instance[ 'title' ];
              }
              else
              {
                  $title = __( 'New title', 'cpf_widget_domain' );
              }
              // Widget admin form
              ?&gt;
              &lt;p&gt;
              &lt;label for="&lt;?php echo $this-&gt;get_field_id( 'title' ); ?&gt;"&gt;&lt;?php _e( 'Title:' ); ?&gt;&lt;/label&gt; 
              &lt;input class="widefat" id="&lt;?php echo $this-&gt;get_field_id( 'title' ); ?&gt;" name="&lt;?php echo $this-&gt;get_field_name( 'title' ); ?&gt;" type="text" value="&lt;?php echo esc_attr( $title ); ?&gt;" /&gt;
              &lt;/p&gt;
              &lt;?php 
          }
  
          // Updating widget replacing old instances with new
          public function update( $new_instance, $old_instance )
          {
              $instance = array();
              $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
              return $instance;
          }
      } // Class cpf_widget ends here
  
      // Register and load the widget
      function cpf_load_widget()
      {
          register_widget( 'cpf_widget' );
      }
  
      add_action( 'widgets_init', 'cpf_load_widget' );
  
  
  ?&gt;
  </code></pre>