5

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');
8
  • What is you function? Edit you question and post function code.
    – Robert hue
    Commented Feb 6, 2015 at 6:07
  • if the function is: function example(){ global $post; return $id=$post->ID; } and is used in single.php, or page.php in general it should return the id of that post/page, and to see that in action you have echo it in there echo example(); Commented Feb 6, 2015 at 6:10
  • @Roberthue I have edited my question Commented Feb 6, 2015 at 6:18
  • @MayeenulIslam actually I do not need to pring anything , I just need to update meta value upon a form submit or when a specific link is clicked . Commented Feb 6, 2015 at 6:20
  • 2
    The init hook is fired before the post id is set, so it will always return false, null or 0 Commented Feb 6, 2015 at 6:25

1 Answer 1

10

The post ID is available after the query has been fired.

The first hook that is safe to get post id is 'template_redirect'.

If you can modify your function to accept a post id as argument, like so:

function em_change_form($id){
    $reg_type = filter_input(INPUT_GET, 'reg_typ', FILTER_SANITIZE_STRING);
    if($reg_type === 'vln'){
      update_post_meta($id,'custom_booking_form', 2);
    } elseif ($reg_type == 'rsvp') {
      update_post_meta($id,'custom_booking_form', 1);
    }
}

You can do:

add_action('template_redirect', function() {
  if (is_single()){
     em_change_form(get_queried_object_id());
  }
});

I've used get_queried_object_id() to obtain current queried post id.

If you absolutely need to call your function on an early hook like 'init', you can use url_to_postid(), and home_url() + add_query_arg() to obtain current url:

add_action('init', function() {
  $url = home_url(add_query_arg(array()));
  $id = url_to_postid($url);
  if ($id) {
     em_change_form($id);
  }
});

Note that second method is less performant because url_to_postid() forces WordPress to parse rewrite rules, so if you can, use the first method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.