ParseError在WordPress多站点
问题描述:
我试图创建一个自定义MultiBlog搜索的WordPress,在那里你可以搜索特定的词,标签,作者,类别,日期范围和注释中使用Ajax的事先键入的内容。ParseError在WordPress多站点
您可以在每个博客搜索,只需选择博客,或者只是一个。结果很好,但我遇到了一些TypeHead自动完成的问题。
对于标签,类别和作者只有我通过Ajax使用TypeHead。
我敢肯定,我是从HTML获取所有值,所以这里是我JS:
var form = $('#multiselect-search-form');
$('#categories').typeahead({
source: function(query, process) {
$.ajax({
url: BaseURL + 'wp-content/plugins/multiblogselect/categories-search.php',
type: 'POST',
data: form.serialize(),
dataType: 'JSON',
async: true,
success: function(data) {
categories = [];
map = {};
$.each(data, function (i, categorie) {
map[categorie.name] = categorie;
categories.push(categorie.name);
});
process(categories);
console.log(categories);
},
error: function(req, err){ console.log('Ajax error ' + err); }
});
}
});
,当我在一个博客只是看(什么的,只是一个选择的)其实它工作正常。但是,当我试图选择大于1名的博客,我越来越parsererror。
因此,这里是我的PHP:
获取价值是好的,我送他们到JS。 $ multiblogselect返回数组中的博客的ID。
if (is_array($multiblogsselect)){
$multiselect = count($multiblogsselect);
if ($multiselect === 1) {
foreach ($multiblogsselect as $key => $value){
if ($value != 1 && $value > 0) {
$tablePrefix = $wpdb->base_prefix.$value.'_';
} else{
$tablePrefix = $wpdb->base_prefix;
}
$resultCategories = getPostCategories($tablePrefix, $categories);
echo json_encode(array_values($resultCategories));
}
} else {
$resultCategories = array();
for ($i=1; $i<=$multiselect; $i++) {
if ($i != 1 && $i > 0) {
$tablePrefix = $wpdb->base_prefix.$i.'_';
} else{
$tablePrefix = $wpdb->base_prefix;
}
$resultCategories = array_merge_recursive($resultCategories, getPostCategories($tablePrefix, $categories));
}
echo json_encode(array_values($resultCategories));
}
}
这样我就可以使用任何前缀用于在数据库中搜索,在一个循环
function getPostCategories($tablePrefix, $categories){
//object_id in term_relationships is post_id
global $wpdb;
$queryCategories = "SELECT name
FROM ".$tablePrefix."term_relationships
JOIN ".$tablePrefix."term_taxonomy
ON ".$tablePrefix."term_relationships.term_taxonomy_id = ".$tablePrefix."term_taxonomy.term_taxonomy_id
JOIN ".$tablePrefix."terms
ON ".$tablePrefix."terms.term_id = ".$tablePrefix."term_taxonomy.term_id
WHERE ".$tablePrefix."terms.name LIKE '%".$categories."%'
AND ".$tablePrefix."term_taxonomy.taxonomy = 'category'
";
$resultCategories = $wpdb->get_results($queryCategories);
return $resultCategories;
}
所以,它工作正常,当我搜索在短短的一个博客,但如果我选择多博客(获取ParseError)。
有什么想法?
编辑:我已经添加了“for”循环,所以我没有parseerror了。但是,多个博客搜索中返回的数组是空的。 array_merge in cause?
答
试试这个语法:
if (is_array($multiblogsselect)){
$multiselect = count($multiblogsselect);
if ($multiselect === 1) {
foreach ($multiblogsselect as $key => $value){
if ($value != 1 && $value > 0) {
$tablePrefix = $wpdb->base_prefix.$value.'_';
} else {
$tablePrefix = $wpdb->base_prefix;
}
$resultCategories = getPostCategories($tablePrefix, $categories);
echo json_encode(array_values($resultCategories));
}
} else {
$resultcategories = array();
foreach ($multiblogsselect as $key => $value) {
if ($value == 1) {
$resultCategories[$key] = getPostCategories($tablePrefix = $wpdb->base_prefix, $categories);
} else {
$resultCategories[$key] = getPostCategories($tablePrefix = $wpdb->base_prefix.$value.'_', $categories);
}
}
echo json_encode(array_values($resultCategories));
}
}