【问题标题】:Why WP_Query's 's'=>'keyword' searches only for 'post_title' and why not 'post_content'?为什么 WP_Query 的 's'=>'keyword' 只搜索 'post_title' 而为什么不搜索 'post_content'?
【发布时间】:2016-11-26 06:11:04
【问题描述】:

我正在为我的自定义帖子编写搜索查询。我为此使用 WP_Query 的搜索参数,然后它必须搜索“post_title”和“post_content”,但它只搜索“post_title”而不搜索“post_content”。以下是我的代码:

 $institute_name = get_user_meta($user_ID, 'inistitute_name', true);
 add_filter( 'posts_where' , array($this,'posts_where' ));
 $paged = (get_query_var('page')) ? get_query_var('page') : 1;
         $args = array(
            'post_type' => 'mypost',
            array( 's' => $keyword ),
            'meta_query' => array (
                array (
                  'key' => 'inistitute_name',
                  'value' => array ($institute_name),
                  'compare' => 'IN'
                )
            ),
            'posts_per_page' => -1,
            'paged' => $paged,
            'post_status' => 'publish',
            );
$the_query = new WP_Query($args);
remove_filter( 'posts_where',  array($this,'posts_where' ));
echo "<pre>";
print_r($the_query);
echo "</pre>";

如果我打印该结果,那么我会得到如下查询:

SELECT wp_dxwe_posts.* FROM wp_dxwe_posts INNER JOIN wp_dxwe_postmeta ON 
( wp_dxwe_posts.ID = wp_dxwe_postmeta.post_id ) WHERE 1=1  AND 
( (wp_dxwe_postmeta.meta_key = 'inistitute_name' AND 
CAST(wp_dxwe_postmeta.meta_value AS CHAR) IN ('ITeLearn') )) 
AND wp_dxwe_posts.post_type = 'mypost' AND ((wp_dxwe_posts.post_status = 'publish')) 
AND post_title LIKE '%launch%' GROUP BY wp_dxwe_posts.ID 
ORDER BY wp_dxwe_posts.post_date DESC 

正如我所说,它仅适用于“post_title”,但为什么它不适用于“post_content”。谁能告诉我这段代码有什么问题?提前致谢。

【问题讨论】:

    标签: php wordpress search


    【解决方案1】:

    如果没有适当的全文索引,搜索整个正文会太慢,以至于在更大的网站上,这最终可能会导致服务器性能下降。

    【讨论】:

    • hmmm.. 首先,感谢您的快速回复。正如我所说,只有“post_title”在工作,如果我尝试使用其他标题,它并不总是有效,那么不会显示结果。如果是这样,请告诉我解决方案是什么?
    猜你喜欢
    • 2021-07-08
    • 2019-05-07
    • 2023-03-15
    • 1970-01-01
    • 2013-04-11
    • 2013-12-03
    • 1970-01-01
    • 2018-10-08
    • 2016-09-23
    相关资源
    最近更新 更多