gpt4 book ai didi

php - posts_search 中的自定义查询

转载 作者:可可西里 更新时间:2023-11-01 06:33:15 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com