This is my function in function.php file
function getcity(){
global $wpdb;
if($_POST['state'])
{
$id=$_POST['state'];
$district = get_post_meta(get_the_ID() , 'district', true);
var_dump($district);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
foreach($result as $row) {
$district_name = $row-
>district_name;
$district_id = $row->district_id;
echo '<option value="'.$district_id.'">'.$district_name.'</option>';
}
}
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
I want to get current post id in this function to display selected dropdown value..
Note that
$post
orget_queried_object_id()
do not work until the first query was fired. So this options are available only at the hooktemplate_redirect
and later. But functions.php is included much earlier (before the hookafter_setup_theme
) so this isn't a solution.A function that should work pretty much anywhere would be
Here is a overview over hook execution order.
If your code is executed after the
template_redirect
hook these option may be better:or
You want the global variable
$post
as documented here:http://codex.wordpress.org/Global_Variables
Declare the
$post
global like so:Then you should be able to access the id of the post using
$post->ID
.Here is a more complete doc of the attributes available through the
$post
global: http://codex.wordpress.org/Function_Reference/$post