Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't retrieve terms with wordpress, when not logged in with ajax request
    primarykey
    data
    text
    <p>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.</p> <p>Here is the code, hope someone can help me.</p> <pre><code>&lt;?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' =&gt; 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' =&gt; 0, 'hide_empty' =&gt; false, )); foreach($regionsParents as $rg){ array_push($regionToExclude, $rg-&gt;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' =&gt; $tmpRegionName, 'hide_empty' =&gt; false, ) ); $regionParent = $tmpRegion[0]-&gt;term_id; //Récupération des départements $departements = get_terms('category',array( 'child_of' =&gt; $regionParent, 'hide_empty' =&gt; false, 'orderby' =&gt; 'name', 'order' =&gt; 'ASC', 'exclude' =&gt; $regionToExclude, )); }else{ //récupération de tous les départements //Récupération des départements $departements = get_terms('category',array( 'hide_empty' =&gt; false, 'orderby' =&gt; 'name', 'order' =&gt; 'ASC', 'exclude' =&gt; $regionToExclude, )); } //Construction et afichage des résultats $options = '&lt;option selected value="wqsftaxoall"&gt;Tous les départements&lt;/option&gt;'; foreach($departements as $dpt){ $options .= '&lt;option value="'.$dpt-&gt;slug.'"&gt;'.$dpt-&gt;name.'&lt;/option&gt;'; } echo $options; die(); } ?&gt; </code></pre> <p>EDIT: It seems the problem is the get_terms function which return an empty array when a not logged user perform tha ajax call. When the user is logged in, the array contains the category list.</p> <p>EDIT2: I still have the problem, searched on google, I can't find anyone with the same problem. I don't know what to do now, maybe i've missed something but i don't know what. help me please :).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload