Expert Group of programming
Loading...

Make wordpress search only in post title

Posted by : Heycode Tech | Posted in : wordpress, WordPress Functions | Mar 4,2017 | 0 comments

As we know that many times,we need to get the search post by title only .Actually we need the exact and accuracy result in our search functionality.

Why would you want to do that?

Actually every user’s (Approximately) need the only desire result in a short listing and maximum time his keyword is related to his : TITLE : that’s the main reason, clients need to search the result via : Title i.e post/page or any CPT title:only .

Suppose if you want to search any product or jobs then surely you will know about the product name i.e product title so you can get the : DESIRE : result in a very short listing and in the same way for the job search also.

Restricting WordPress Search to Titles Only ?

Yes, it is right.Now WordPress search provides a lot of search listing so for finding the desire search, it will takes some times.So By restricting search results(By title search (‘page/post/cpt/) ), many extraneous matches can be filtered out.

So for enhancing this functionality, we need to follow the different way. Here we are describing here one by one.

1: Limit Search to Post/Page/CPT Titles Only : By WP_QUERY ('global $wp_query;')

See the code.

$paged = get_query_var( 'paged', 1 );
//your search query
$search_term  = 'The'; //replace with search query
$wpqury_args = array(
    'post_type' => 'page', //use any post type like post,or any custom post type also.
    'posts_per_page' => 10,
    'paged' => $paged,
    'search_prod_title' => $search_term,
    'post_status' => 'publish',
    'orderby'     => 'title', 
    'order'       => 'ASC'
);

add_filter( 'posts_where', 'title_filter', 10, 2 );
$loop = new WP_Query($wpqury_args);
remove_filter( 'posts_where', 'title_filter', 10, 2 );

echo '<ul>';
if($loop->have_posts()):
while($loop->have_posts()): $loop->the_post();

echo '<li>';
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
echo '</li>';

endwhile;
endif;
echo '</ul>';
wp_reset_query();


//past this code in 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;
}


Explanation:
i: Meta Key : 'search_prod_title' => $search_term is uses for search the post by the title. It is the WP default functionality.

ii: For this meta key : ‘search_prod_title’ : it is not the custom key . It is the default WP key so don’t be confused.

2:Limit Search to Post Titles Only | By Filter – posts_search

By using of this way,You will modify the : Default search.php : file. So for using it,you need to only past the code in one file : functions.php : and your other file : search.php : as the same. It means your don’t modify the : search.php :file but It is the required file and in your theme this file must exists.

This code is not related to the code above and can be used independently of it.

Here is the code.

//Search by  post title 
function heycodetech_search_by_title_only( $search, &$wp_query )
{
    global $wpdb;
    if ( empty( $search ) )
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;//query var
    $n = ! empty( $q['exact'] ) ? '' : '%';
    $search =
    $searchand = '';
    foreach ( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if ( ! empty( $search ) ) {
        $search = " AND ({$search}) ";
        if ( ! is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter( 'posts_search', 'heycodetech_search_by_title_only', 500, 2 );




So by using of this way, you can limit the search functionality and allow only : post title : .

3: Exclude some posts in Default WP Search

As we know that sometime, we need to exclude some posts in default WP search functionality. So for doing this,we need to add only a simple function in file : functions.php : .

This code is not related to the code above and can be used independently of it.

Suppose if you want to exclude the post : ids – 22,12,60 : then add this post id which describe in below.

function ni_search_filter( $query ) {
  if ( $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 22,12,60 ) );
  }
}
add_filter( 'pre_get_posts', 'ni_search_filter' );

Now when you are using this code and feel any difficulties then please comment on below comment.I will try to resolved your issues.
I hope to all for sharing the knowledge.

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and moreHere is the Expert group of programming.