2
I am trying to pull registered cities through the selected state. For example:
The right thing would be to pull the cities registered in the Wordpress, before I could make it work, but I use the advanced custom field
, and after updating it I can’t figure out why it’s not pulling. I’ve tried everything.
I’ll leave the code for you to analyze :
//Load cities - Convenio
function ag_apcef_load_cities_field_choices( $field ) {
// get selected
$selected = get_field( 'ag-estado-convenio', $post->ID );
// reset choices
$field['choices'] = array();
if($selected) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected->ID
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$field['choices'][$post->ID] = $post->post_title;
}
}
return $field;
}
add_filter('acf/load_field/name=ag-cidade-convenio', 'ag_apcef_load_cities_field_choices');
// Admin enqueue - Convenio
function ag_apcef_admin_convenio_enqueue( $hook ) {
// Allowed post types
$types = array( 'convenio' );
if( !in_array( get_post_type(), $types ) )
return;
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate-admin.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
)
);
}
add_action( 'admin_enqueue_scripts', 'ag_apcef_admin_convenio_enqueue' );
//Enqueue - Convenio
function ag_apcef_convenio_enqueue() {
if ( is_page( 'portal-de-vantagens' ) || is_page( 'bem-estar' )) {
wp_enqueue_script( 'populate-area', get_stylesheet_directory_uri() . '/js/autopopulate.js' );
wp_localize_script( 'populate-area', 'populate_vars',
array(
'populate_nonce' => wp_create_nonce( 'populate_nonce' ), // Create nonce
'ajaxurl' => admin_url( 'admin-ajax.php' )
)
);
}
}
add_action( 'wp_enqueue_scripts', 'ag_apcef_convenio_enqueue' );
// Load cities by state
function ag_apcef_city_by_state( $selected_state ) {
// Verify nonce
if( !isset( $_POST['populate_nonce'] ) || !wp_verify_nonce( $_POST['populate_nonce'], 'populate_nonce' ) )
die('Permission denied');
// Get selected state
$selected_state = $_POST['state'];
// Populate arr_data
$arr_data = array();
if(!empty($selected_state)) {
$args = array(
'post_type' => 'cities',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'city_meta_box_state',
'value' => $selected_state
)
)
);
$posts = get_posts( $args );
foreach($posts as $post) {
$arr_data[] = [
'key' => $post->ID,
'value' => $post->post_title
];
}
}
return wp_send_json($arr_data);
die();
}
add_action('wp_ajax_apcef_city_by_state', 'ag_apcef_city_by_state');
add_action('wp_ajax_nopriv_apcef_city_by_state', 'ag_apcef_city_by_state');
I’m completely lost with this update...
Apparently no one can help
– Axcse
do you have any errors? if you were unable to identify any errors, press F12 and see what happens after entering the page or changing the select!
– Lodi
There appears to be no error.
– Axcse
select city becomes empty? no city appears or just not filtering correctly?
– Lodi
So finally I managed to solve, the problem is in the same bank structure, when updating the plugin the same changed a few things ( use the Custom field Advanced ) ai just had to read all the documentation of the same and solved.
– Axcse