php - posts_search 中的自定义查询

标签 php mysql wordpress

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

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 中我收到消息,没有结果。

最佳答案

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

关于php - posts_search 中的自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32031307/

相关文章:

php - 如何在 PHP 中将上传的视频文件编码为 .flv 格式

html - 如何解决 WordPress 中的传入电子邮件错误

php - jquery 发布和响应操作

php - 在插入 MySQL 之前使用 htmlspecialchars() 好吗?

mysql - Docker wordpress/mysql 失败并显示 "connection refused"

php - WordPress 网站 <a href =""></a> 无法使用 lightbox

css - Safari 5.1 将边距/填充推出超过 50+ 像素

php - 请求类 Laravel 5.2

php - 如何检测照片的拍摄角度,并像桌面应用程序一样在浏览时自动旋转网站显示?

php7 无效返回类型不起作用?