get the current page id inside wordpress plugin page

I need to get the current page id in WordPress plugin page outside the loop. And the code I wrote for getting current page id is in my plugin page. I tried many codes, but doesn’t work

$page_object = get_queried_object();
$page_id     = get_queried_object_id();


 // "Dirty" pre 3.1
 global $wp_query;

$page_object = $wp_query->get_queried_object();
$page_id     = $wp_query->get_queried_object_id();

But it doesn’t work for me .

Read More
  global $post;
  echo "pageid: ".$post->ID;

This is also not working.

When I try

     global $wp_query;
     $post_obj = $wp_query->get_queried_object();
     $Page_ID = $post_obj->ID;
     echo $Page_ID;

Then a error message appears

Fatal error: Call to a member function get_queried_object()
on a non-object in
H:xampphtdocswordpresswp-contentpluginswpkwpk.php
on line 876

When I print:

global $wp_query;
print_r($wp_query);

then result is:

WP_Query Object
(
    [query] => 
    [query_vars] => Array
        (
        )

    [tax_query] => 
    [meta_query] => 
    [date_query] => 
    [queried_object] => 
    [queried_object_id] => 
    [request] => 
    [posts] => 
    [post_count] => 0
    [current_post] => -1
    [in_the_loop] => 
    [post] => 
    [comments] => 
    [comment_count] => 0
    [current_comment] => -1
    [comment] => 
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] => 
    [is_preview] => 
    [is_page] => 
    [is_archive] => 
    [is_date] => 
    [is_year] => 
    [is_month] => 
    [is_day] => 
    [is_time] => 
    [is_author] => 
    [is_category] => 
    [is_tag] => 
    [is_tax] => 
    [is_search] => 
    [is_feed] => 
    [is_comment_feed] => 
    [is_trackback] => 
    [is_home] => 
    [is_404] => 
    [is_comments_popup] => 
    [is_paged] => 
    [is_admin] => 
    [is_attachment] => 
    [is_singular] => 
    [is_robots] => 
    [is_posts_page] => 
    [is_post_type_archive] => 
    [query_vars_hash] => 
    [query_vars_changed] => 1
    [thumbnails_cached] => 
    [stopwords:WP_Query:private] => 
)

I don’t know how to solve this, how to get the current page id.If you know how to solve this, then I need your support. Thanks in advance.

Related posts

Leave a Reply

9 comments

  1. get_the_ID(); or $post->ID; returns the current page or post id in WordPress.

    But you need to ensure that your post is saved in wordpress post
    table. Other wise you can’t get the id , simply because of it is
    not an entry in wordpress database.

    If it is a static page and it’s not an entry in wordpress post then, get_the_ID() didn’t return anything.

    For example : get_the_ID() didn’t go to work in post archive pages , administration pages in wordpress backend etc.

    So as per this question you are trying to get the id of the page that is a backend plugin setting page or any archive page .

    UPDATE

    Method to get the current post id in wordpress

    (1) global $post; $post->ID();

    (2) global $wp_query; $post_id = $wp_query->get_queried_object_id();

    (3) global $wp_query; $post_id = $wp_query->post->ID;

    (4) get_the_ID();

    [ It is recommended that this tag must be within The Loop. ]

    see this

          function get_the_ID() {
                   $post = get_post();
                   return ! empty( $post ) ? $post->ID : false;
                    }
    

    ie get_the_ID() return the id of current $post .

    (5) get_query_var('page_id')

    [ it will not going to work if we use pretty permalink ]
    https://codex.wordpress.org/Function_Reference/get_query_var

  2. You can get ID of the post in current page outside the loop using the technique below:

    global $wp_query;
    $post_id = $wp_query->post->ID;
    
    $post = get_post( $post_id );
    $slug = $post->post_name;
    
  3. I guess that this is the proper solution:

    $id = get_queried_object_id();
    

    which equals to:

    function get_queried_object_id() {
        global $wp_query;
        return $wp_query->get_queried_object_id();
    }
    
  4. Chosen answer is working only if you put it in the WordPress loop. Outside it will render useless.

    This is working everywhere:

    global $wp_query;
    $postID = $wp_query->post->ID;
    
  5. Above solutions did not works for me. I did it like this and works for me.

    add_action ( 'after_setup_theme', 'custom_function' );
    function custom_function() {
        $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        // get page id from url and check for ssl enabled 
        $pageId = url_to_postid(set_url_scheme($current_url));
        
    } 
    
  6. You get see all of the settings and variables in get_defined_vars() function:

    var_dump(get_defined_vars());
    

    In your case you need to get ‘_GET’ and inside ‘post’… The code should look like this:

    $tmp = get_defined_vars();
    var_dump($tmp['_GET']['post']);
    
  7. For those of you who this still isn’t working for, you will need to use some sort of add_action (you can choose which one you want to use). For my example, this will return the current page ID without any problems, regardless of whether in a plugin folder, functions php, or elsewhere.

    add_action('template_redirect', 'showid');
    
    function showid(){
        global $wp_query;
        $theid = intval($wp_query->queried_object->ID);
        echo $theid;
    }
    
  8. Nothing seemed to work for me until I tried this

    <?php
    
    function child_pages() {
      
        $page_id = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
    
        echo "<h2>" . $page_id . "</h2>";
    
    }
    
    add_action( 'template_redirect', 'child_pages' );
    ?>