I need the current post id in a function , which I have written in functions.php. But I can't get the id. I have tried several method.
Like
get_the_ID(); //returns false
global $post;
$id = $post->ID; //returns null
global $wp_query
$id =$wp_query->get_queried_object_id(); //returns 0
$url = 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$id = url_to_postid($url); //returns 0
I am using the latest version of wordpress . What can I do now?
UPDATE: I need post id in the function below.
function em_change_form(){
$id = get_the_ID();
if(isset($_GET['reg_typ'])) {
$reg_type = $_GET['reg_typ'];
if($reg_type =='vln'){
update_post_meta($id,'custom_booking_form', 2);
} elseif ($reg_type == 'rsvp') {
update_post_meta($id,'custom_booking_form', 1);
}
}
}
add_action('init','em_change_form');
function example(){ global $post; return $id=$post->ID; }
and is used insingle.php
, orpage.php
in general it should return the id of that post/page, and to see that in action you have echo it in thereecho example();