代码之家  ›  专栏  ›  技术社区  ›  rob.m

如何查询只搜索文章标题?

  •  0
  • rob.m  · 技术社区  · 6 年前

    $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,则意味着用户只想按TITLE搜索$term,而不想搜索其他内容,但是用户可以选择按类别搜索,因此我无法覆盖整个查询。我只需要一个特定的职位名称查询

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gufran Hasan    6 年前

    你可以从这个链接获得帮助 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;
        }