posts_search中的自定义查询 [英] Custom query in posts_search

查看:249
本文介绍了posts_search中的自定义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此查询用作自定义搜索查询?

How can I use this query as my custom search query?

add_filter('posts_search', 'my_search_is_perfect', 20, 2);
function my_search_is_perfect($search, $wp_query) 
{
   $sWord = 'Zukunft haus';

   return "
       SELECT *, 
              MATCH(post_title) AGAINST('$sWord' IN BOOLEAN MODE) AS Score 
        FROM `wp_posts` 
       INNER JOIN wp_term_relationships ON wp_term_relationships.object_id = ID
             AND wp_term_relationships.term_taxonomy_id = 1
       WHERE MATCH( post_title) AGAINST ('$sWord' IN BOOLEAN MODE) 
         AND `post_status` = 'publish'
         AND `post_type` = 'post'
       ORDER BY score DESC
  "; 
}

查询是正确的(我在phpMyAdmin中进行了检查),但在WordPress中却收到了消息,没有结果.

The query is correct (I checked this in phpMyAdmin) but in WordPress I get the message, no results.

推荐答案

在function.php文件中:

add_filter('posts_search', 'my_search_is_perfect', 20, 2);
function my_search_is_perfect() 
{
    global $post;
    global $wpdb;
    $sWord = 'Zukunft haus';

    $sel_query = "SELECT *, 
                          MATCH(post_title) AGAINST('$sWord' IN BOOLEAN MODE) AS Score 
                    FROM ".$wpdb->prefix."posts 
                   INNER JOIN ".$wpdb->prefix."term_relationships ON ".$wpdb->prefix."term_relationships.object_id = ID
                         AND ".$wpdb->prefix."term_relationships.term_taxonomy_id = 1
                   WHERE MATCH( post_title) AGAINST ('$sWord' IN BOOLEAN MODE) 
                     AND post_status = 'publish'
                     AND post_type = 'post'
                   ORDER BY score DESC";
    $totaldata = $wpdb->get_results($sel_query);

    return $totaldata;
}

这篇关于posts_search中的自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆