用自定义外部查询替换搜索结果

时间:2017-05-16 作者:Rod0n

我想用外部查询(来自Solr)替换现有的搜索功能,但在search.php 页这是我的密码functions.php:

add_action(\'pre_get_posts\', \'my_search_query\');

function my_search_query($query) {
    if($query->is_search() && $query->is_main_query() && get_query_var(\'s\', false)) {   

    $desired_query = get_query_var(\'s\', false);

    $url = \'http://{SOLR_SERVER}:8983/solr/{CORE}/select?indent=on&q=\' . $desired_query . \'~2&wt=json\';
    $result = file_get_contents($url);
    $data = json_decode($result, true);

    $ids = array();
    foreach ($data[\'response\'][\'docs\'] as $item)
    {
        array_push($ids, $item[\'id\']);
    }

    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $query = new WP_Query(array(
        \'post__in\' => $ids,
        \'post_type\' => array(\'last-news\',\'blog\'), 
        \'paged\' => $paged, 
        \'posts_per_page\' => 5,
    ));

}
return $query;
}
这是search.php:

<section class="content">
    <div class="page-title pad group">
        <h1>
        <?php if ( have_posts() ): ?><i class="fa fa-search"></i><?php endif; ?>
        <?php if ( !have_posts() ): ?><i class="fa fa-exclamation-circle"></i><?php endif; ?>
        <?php echo $wp_query->found_posts  ?>
        <?php _e(\'Search Results\',\'theme\'); ?>
        </h1>
    </div>

    <div class="pad group">

        <div class="notebox">
            <?php _e(\'Found\',\'theme\'); ?> "<span><?php echo get_search_query(); ?></span>".
            <?php if ( !have_posts() ): ?>
                <?php _e(\'Try with another term:\',\'theme\'); ?>
            <?php endif; ?>
            <div class="search-again">
                <?php get_search_form(); ?>
            </div>
        </div>

        <?php 
            if ( have_posts() ) : ?>

            <div class="post-list group">
                <?php $i = 1;  while ( have_posts() ): the_post(); 
                echo \'<div class="post-row">\';
                 get_template_part(\'content\'); echo \'</div>\'; endwhile;  ?>
            </div><!--/.post-list-->

            <?php get_template_part(\'inc/pagination\'); ?>

        <?php endif; ?>

    </div><!--/.pad-->

</section><!--/.content-->
回声$wp_query->found_posts 返回数字“100”,因此它似乎得到了一些东西,但have_posts() 函数返回假值。

我做错了什么?我只想在$ids 数组每当搜索完成时,我不需要其他过滤器。

Edit (alternative solution):

我也可以这样做:

add_action(\'pre_get_posts\', \'my_search_query\');
function my_search_query($query) {
    if($query->is_search() && $query->is_main_query() && get_query_var(\'s\', false)) {   

    $desired_query = get_query_var(\'s\', false);

    $url = \'http://{SOLR_SERVER}:8983/solr/{CORE}/select?indent=on&q=\' . $desired_query . \'~2&wt=json\';
    $result = file_get_contents($url);
    $data = json_decode($result, true);

    $ids = array();
    foreach ($data[\'response\'][\'docs\'] as $item)
    {
        array_push($ids, $item[\'id\']);
    }

    $query->set(\'post__in\', $ids );
}
return $query;
}
这一个的问题是,正如我所理解的,搜索将与搜索词一起运行,并且只会返回与特定帖子ID中的词匹配的帖子。我不想做这个操作,因为我已经用solr做过了。此外,Solr允许我进行模糊查询,但在该设置中这不起作用,因为wordpress搜索将返回0个结果,而找不到确切的术语。

2 个回复
SO网友:Milo

无法从返回查询对象pre_get_posts 行动事实上,它不应该返回任何内容,查询对象是通过引用传递的。

您的第二次尝试已接近成功,您可以取消设置s 查询var,以便WordPress不会尝试搜索该术语。当然,现在s 没有值,因此任何尝试输出搜索项的函数都只会得到一个空字符串。

add_action(\'pre_get_posts\', \'my_search_query\');
function my_search_query($query) {
    if($query->is_search() && $query->is_main_query() && get_query_var(\'s\', false)) {
        // get your $ids, then
        unset( $query->query_vars[\'s\'] );
        $query->set( \'post__in\', $ids );
    }
}

SO网友:Brandonrugzie

下面是关于答案的更多信息,并对我们在每个步骤中所做的工作以及如何将其转换为WordPress数据库中的工作进行了一些评论

#run before posts are grabbed from DB
add_action(\'pre_get_posts\', \'my_search_query\');
function my_search_query($query) {
    #global $wpdb; # used for internal with WP searching
    #is this query the main search query, and is there a search query provided?
    if($query->is_search() && $query->is_main_query() && get_query_var(\'s\', false)) {

        #fun SQL goes here

            #INTERNAL
            #$sql = "" #SQL query
            #$results = $wpdb->get_results( $sql ); # used for internal with WP searching

            #EXTERNAL
            #$url = \'http://yourapi.com/json\'; #go out to an api or something
            #$result = file_get_contents($url);
            #$results = json_decode($result, true);

        #create array to hold returned ids
        $ids = array();
        #loop through each and grab ID
        foreach ($results as $item) {
            array_push($results, $item[\'id\']);
        }
        #make sure there are posts if you\'re going to replace search
        if( ! empty( $ids )) { 
            #Tell original WP search to shove it
            unset( $query->query_vars[\'s\'] );
            #replace the current query with your results
            $query->set( \'post__in\', $ids );

        }

    }
    #send back the query whether we touched it, or it didn\'t match our parameters
    return $query;
}

结束

相关推荐

How to customize user search

我正在开发一个自定义用户搜索,我正在使用找到的这个插件here:<?php function sul_user_listing($atts, $content = null) { global $post; extract(shortcode_atts(array( \"role\" => \'technician\', \"number\" => \'10\' )