How to modify a global variable in a function and use it on another function?

I can access the post id in this function:

global $myId;
function myFunction( $post_object ) {
    global $post;
    global $myId;
    $myId = $post->ID;
    echo $myId; //Prints the post ID successfully   
}
add_action( 'the_post', 'myFunction' );

But when I try to use this global variable in another function, the variable is null.

function do_something()
{
    global $myId;
    echo $myId;   //This prints NULL, when i sent it via JQuery to a JS function.

}

I tried multiple ways to define the global variable, but still I can’t reach the modified version of the variable. How can i achieve this?

Read more here: How to modify a global variable in a function and use it on another function?

Leave a Reply

Your email address will not be published. Required fields are marked *