如何仅查询帖子标题?

时间:2018-11-13 10:11:25

标签: php wordpress

我尝试过此方法,但我一直收到所有帖子

$post_ids_post = $wpdb->get_col( $wpdb->prepare(
    "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_title LIKE '%s'", 
    $search, $search 
));
$tit = array(
    'post__in' => $post_ids_post,
    's' => $s
);

我正在通过GET将var发送到搜索结果页面,如果表单已选中TITLE,则意味着用户只想按标题搜索$ term,而没有别的,但是用户可以选择按类别搜索,因此我无法覆盖整个查询。我只需要针对帖子标题的特定查询

1 个答案:

答案 0 :(得分:0)

您可以从此链接http://heycodetech.com/make-wordpress-search-only-in-post-title/

获得帮助
//put this code in the functions.php file.
    function title_filter( $where, &$wp_query )
    {
        global $wpdb;
        if ( $search_term = $wp_query->get( 'search_prod_title' ) ) {
            $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
        }
        return $where;
    }