問題描述
有一個自動完成欄位工作幾個月,但是沒有登入時停止工作?不知道什麼時候,但在最近幾天或周內 (最近沒有更新的 wordpress) 。
已經有; add_action(‘wp_ajax_filter_schools’,’filter_schools’); add_action(‘wp_ajax_nopriv_filter_schools’,’filter_schools’);
在 functions.php 中,沒有任何錯誤。
沒有登入的時候得到的答覆是來自 safari … *請求 URL:http://www.payingforit.org.uk/wp-admin/admin-ajax.php?term = holywe& action = filter_schools& postType =學校請求方法:GET 狀態碼:302 找到*
任何幫助歡迎!特區。
jquery 程式碼
$( "#userSelectedSchool" ).bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
}).autocomplete({
source: function( request, response ) {
$.getJSON( "/wp-admin/admin-ajax.php", {
term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
}, response );
dataToBeSent = {
term: extractLast( request.term ), action: 'filter_schools', postType: 'school'
}
console.log(request.term);
}, select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.id );
// add placeholder to get the comma-and-space at the end // ui.item.label
terms.push( "" );
this.value = ui.item.label;
$('input[name=userSchool]').val(ui.item.urn)
return false;
}, open: function() { $('.ui-menu').width(300) }
});
function in functions.php
add_action('wp_ajax_filter_schools', 'filter_schools');
add_action('wp_ajax_nopriv_filter_schools', 'filter_schools');
function filter_schools(){
global $wpdb; // this is how you get access to the database
$str = $_GET['term'];
$action = $_POST['action'];
$postType = $_POST['postType'];
$finalArgs = array (
'posts_per_page'=>5,
'order' => 'ASC',
'post_type' => 'school'
);
$searchSchools = new WP_Query( $finalArgs );
$mypostids = $wpdb->get_col("select ID from $wpdb->posts where post_title LIKE '".$str."%' ");
$args = array(
'post__in'=> $mypostids,
'post_type'=>'school',
'orderby'=>'title',
'order'=>'asc'
);
$res = new WP_Query($args);
while( $res->have_posts() ) : $res->the_post();
global $post;
$EstablishmentNumber = get_post_meta($post->ID,'EstablishmentNumber', true);
$URN = get_post_meta($post->ID,'URN', true);
$add = get_post_meta($post->ID,'address', true);
$schl = array('post_id'=>$post->ID,'id'=>$EstablishmentNumber, 'label'=>$post->post_title.', '.$add['town'].' '.$add['postcode'] , 'value'=>$EstablishmentNumber, 'urn'=>$URN );
$matchedSchools[] = $schl;
endwhile;
echo json_encode($matchedSchools);
wp_reset_postdata();
die(); // this is required to return a proper result
}
最佳解決方案
編輯:我在下面保留了我的原始答案,但是,我不知道我在想什麼… 你不應該需要觸發 do_action( 'wp_ajax...' )。
雖然我不能確定問題是什麼,是問題的程式碼大致上是 (我認為 $_POST 應該是 $_GET 與.getJSON) 。
嘗試把它放在頂部…
if(isset($_REQUEST['action']) && $_REQUEST['action']=='filter_schools'):
do_action( 'wp_ajax_' . $_REQUEST['action'] );
do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
endif;
我認為 WordPress 不會自動為不是 logged-in 的使用者執行 ajax 操作。潛在的 non-users 可能不能假定能夠做到這一點。
我可能會改變這些 $_GET s& %_POST 也可以用於 $_REQUEST 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。