Ajax call when not logged in, get_terms return empty array
-
hello! I’ve got a strange problem.
I have two html list, when i choose a region, it sends an ajax request to wordpress and returns the list of this region’s province.
The problem is that the get_terms() function of wordpress doesn’t retrieve any province when we are not logged in, even if he correctly received the data. The value $_POST[‘region’] is correct even if you’re not connected.I may have made some mistakes, i’m french and i apologize.
Here is the code, hope someone can help me.
<?php /* Plugin Name: Ajax Départements Description: Récupère les sous categories(départements) d'une categorie(région) Version: 0.1 Author: EnessFr */ //Enregstrement du trigger et du callback add_action( 'wp_ajax_dpt-ajax-request', 'ajaxGetDepartement' ); add_action( 'wp_ajax_nopriv_dpt-ajax-request', 'ajaxGetDepartement' ); //insertion du script wp_enqueue_script( 'dpt-ajax-request', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ), null, true ); //Enregistrement de l'url ajax wp_localize_script( 'dpt-ajax-request', 'dptAjaxRequest', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); //callback ajax function ajaxGetDepartement(){ $departements = null; $regionToExclude = array(); $tmpRegionName = $_POST['region']; //Récupération des régions parents $regionsParents = get_terms('category',array( 'parent' => 0, 'hide_empty' => false, )); foreach($regionsParents as $rg){ array_push($regionToExclude, $rg->term_id); } array_push($regionToExclude, 1); // 1 = catégorie non-classé //Récupération de la région passée en arguments if(strval($_POST['region']) != 'wqsftaxoall'){ // une région valable est re?us $tmpRegion = get_terms('category',array( 'slug' => $tmpRegionName, 'hide_empty' => false, ) ); $regionParent = $tmpRegion[0]->term_id; //Récupération des départements $departements = get_terms('category',array( 'child_of' => $regionParent, 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'exclude' => $regionToExclude, )); }else{ //récupération de tous les départements //Récupération des départements $departements = get_terms('category',array( 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', 'exclude' => $regionToExclude, )); } //Construction et affichage des résultats $options = '<option selected value="wqsftaxoall">Tous les départements</option>'; foreach($departements as $dpt){ $options .= '<option value="'.$dpt->slug.'">'.$dpt->name.'</option>'; } echo $options; die(); } ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Ajax call when not logged in, get_terms return empty array’ is closed to new replies.