• hi,

    I have a page that calls my custom plugin. In this plugin I am trying to get the current page ID,

    function hook_ajax_register() {
    global $wp_query;
    check_ajax_referer($this->nonce_key_register, '_wpnonce', true); // die on failure
    // Get the form data
    parse_str($_POST['data'], $data);
    
    $thePostID = $wp_query->post->ID;
    $this->ref_code = get_post_meta($thePostID, 'ReferenceID', true);
    
    .....etc
    }

    If I change the post Id to a particular post(say 75) it works, however trying to retrieve the current post id where the plugin is called is not working!
    Please help

Viewing 6 replies - 1 through 6 (of 6 total)
  • global $post;
    $postID = $post->ID;
    Thread Starter csam0003

    (@csam0003)

    this doesnt seem to work aswell!
    Could it be that it is because Im calling it within the plugin?

    I don’t know. Have you tried print_r($post) and see what comes out? When stuff doesn’t do what I expect, I find var_dump() or print_r() helps me figure it out immensely – let’s me see what’s being grabbed, and if I’m calling the incorrect variable.

    Thread Starter csam0003

    (@csam0003)

    Thanks for your reply!
    I did the following

    function hook_ajax_register() {
    global $wp_query
    $thePostID = $wp_query->post->ID;
    $this->marketing_code = get_post_meta($thePostID, 'ReferenceID', true);

    However I am now getting a zero value?

    any ideas?

    Thanks again

    first off, you’re missing a semi-colon after your global.

    global $wp_query

    should be

    global $wp_query;

    Secondly, what I suggested is you globalize the $post – you’re not doing that, so $post->ID will result in nothing.

    function hook_ajax_register() {
    global $post;
    $thePostID = $post->ID;
    $this->marketing_code = get_post_meta($thePostID, 'ReferenceID', true);

    try that.

    Hi,

    I am using below code in my plugin:

    global $post;
    $postID = $post->ID;

    This is not under any function, but this seems to not work… This is also not working:

    get_the_ID();

    Is there something i am missing (file, function etc) ?

    Any help on this will be appreciated…

    Thanks,
    Harpinder

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Get current page id in custom plugin’ is closed to new replies.