php - 在 WordPress 多站点中使用 Typeahead Ajax 时出现 ParseError

标签 php jquery json ajax wordpress

我正在尝试为 WordPress 创建自定义 MultiBlog 搜索,您可以在其中搜索特定单词、标签、作者、类别、日期范围和评论。

您可以搜索每个博客、仅搜索选定的博客或仅搜索一个博客。结果很好,但我在 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 个博客时,我收到了解析器错误。

这是我的 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”循环,所以我不再有解析错误。但是,在多个博客搜索中返回的数组为空。 array_merge 的原因是什么?

最佳答案

尝试这个语法:

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));
    }
}

关于php - 在 WordPress 多站点中使用 Typeahead Ajax 时出现 ParseError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34509695/

相关文章:

php - 在 Laravel 中按数据透视表 created_at 排序

php - Yii2 #403 错误仅在 IE 中出现,并且在兼容性 View 模式打开时也会出现

Javascript 函数并不总是在元素准备好后加载

php - 无序列表-列表项位置

java - 不要使用android内置的org.json

java - 如何使用 JavapreparedStatement 将 JSON 对象插入 Postgres?

python - 如何使用 itertools.groupby 格式化字典列表?

javascript - 使用 PHP 和 JS 将 div = contenteditable 保存到 MySQL

javascript - 当用户使用 JavaScript 在文本框中键入内容时清除单选按钮值

javascript - jQuery:通过单击按钮导航到另一个页面