Skip to content

Instantly share code, notes, and snippets.

@codersaiful
Created August 10, 2020 04:35
Show Gist options
  • Save codersaiful/12ab6d0fb53ccb2ebfdd9c449dce40a9 to your computer and use it in GitHub Desktop.
Save codersaiful/12ab6d0fb53ccb2ebfdd9c449dce40a9 to your computer and use it in GitHub Desktop.
if( !function_exists( 'load_terms_by_ajax' ) ) {
function load_terms_by_ajax(){
$ky = isset( $_GET['q'] ) && !empty( $_GET['q'] ) ? $_GET['q'] : '';
$term_name = isset( $_GET['key'] ) && !empty( $_GET['key'] ) ? $_GET['key'] : '';
$texonomy_sarch_args = array('hide_empty' => true,'orderby' => 'name','order' => 'ASC', 'name__like' => $ky, );
$tax_obj = get_terms( $term_name, $texonomy_sarch_args );
$arr['results'] = array();
if( count( $tax_obj ) > 0 ){
foreach( $tax_obj as $item ){
$arr['results'][] = array(
'id' => $item->term_id,
'text' => $item->name,
);
}
}
echo json_encode($arr);
die();
}
}
add_action( 'wp_ajax_load_terms_by_ajax', 'load_terms_by_ajax' );
add_action( 'wp_ajax_nopriv_load_terms_by_ajax', 'load_terms_by_ajax' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment