Reputation: 3226
I need to get the id for a custom post in functions.php
.
I tried the following but none are working:
$post_id = get_the_ID ()
global $post;
$post_id = $post->ID;
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
What I'm doing is sending data with jquery to a functions and I need the id here:
if ( isset( $_POST["tab_id"] ) ) {
$url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$ID = url_to_postid($url[0]);
var_dump($ID);
$user_ID = get_current_user_id();
$project_id = $_POST['post_id'];
var_dump( $project_id );
$tab_id = $_POST['tab_id'];
//form processing code here
}
Upvotes: 0
Views: 1540
Reputation: 4244
You're trying to get it at ajax handler function. At that time, none of these function will work be it built in post type or custom post type. Easiest solution would be to pass ID along with your ajax request.
Another solution is to use url_to_postid
with wp_get_referer
. You can check docs for these functions here and here. I wouldn't recommend this one though.
Upvotes: 0
Reputation: 441
There is no post data available at this point. If you have to get the post Id here, you could try extracting it from the url:
$url = explode('?', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$ID = url_to_postid($url[0]);
Upvotes: 1