Skip to main content
deleted 21 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 *
 * @link    http://wordpress.stackexchange.com/a/11826/1685
function __search_by_title_only(*
 * @param   string      $search, 
 &$wp_query* )@param   WP_Query    $wp_query
{ */
function wpse_11826_search_by_title( $search, $wp_query global) $wpdb;
{
    if ( ! empty( $search ) )
&& ! empty( $wp_query->query_vars['search_terms'] ) ) {
  return $search; // skip processing - noglobal search$wpdb;

 term in query

     $q = $wp_query->query_vars; 
    
     $n = ! empty( $q['exact'] ) ? '' : '%';

    $search =
    $searchand$search = '';array();

        foreach ( ( array ) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search$search[] .= "{$searchand}($wpdb->prepare( "$wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
    %s", $n . $wpdb->esc_like( $searchand$term =) '. AND$n ';);

    }

    if ( ! emptyis_user_logged_in( $search ) ) {
        $search = " AND ({$search}) ";
        if$search[] (= !"$wpdb->posts.post_password is_user_logged_in()= )''";

        $search = ' AND $search' .= "implode( ' AND ($wpdb->posts.post_password', =$search '') ";;
    }

    return $search;
} 

add_filter( 'posts_search', '__search_by_title_only''wpse_11826_search_by_title', 50010, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

UPDATE: This code has been updated for WordPress 3.2.1. I'm surprised it even worked before asRemoved deprecated $wpdblike_escape() wasn't global!since 4.0

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 */
function __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;    
     $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', '__search_by_title_only', 500, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

UPDATE: This code has been updated for WordPress 3.2.1. I'm surprised it even worked before as $wpdb wasn't global!

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 *
 * @link    http://wordpress.stackexchange.com/a/11826/1685
 *
 * @param   string      $search 
 * @param   WP_Query    $wp_query
 */
function wpse_11826_search_by_title( $search, $wp_query ) {
    if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
        global $wpdb;

        $q = $wp_query->query_vars; 
        $n = ! empty( $q['exact'] ) ? '' : '%';

        $search = array();

        foreach ( ( array ) $q['search_terms'] as $term )
            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );

        if ( ! is_user_logged_in() )
            $search[] = "$wpdb->posts.post_password = ''";

        $search = ' AND ' . implode( ' AND ', $search );
    }

    return $search;
} 

add_filter( 'posts_search', 'wpse_11826_search_by_title', 10, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

UPDATE: Removed deprecated like_escape() since 4.0

deleted 104 characters in body
Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 */
function __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;

    // wp-includes/query.php line 2128 (version 3.1)
    $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 ';
    }
    
    $term = esc_sql( like_escape( $q['s'] ) );
    if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
        $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

    if ( !empty( $search ) ) {
        $search = " AND ({$search}) ";
        if ( ! is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    
    return $search; 
}
add_filter( 'posts_search', '__search_by_title_only', 10500, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

UPDATE: This code has been updated for WordPress 3.2.1. I'm surprised it even worked before as $wpdb wasn't global!

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 */
function __search_by_title_only( $search, &$wp_query )
{
    if ( empty($search) )
        return $search; // skip processing - no search term in query
    
    $q =& $wp_query->query_vars;

    // wp-includes/query.php line 2128 (version 3.1)
    $n = !empty($q['exact']) ? '' : '%';
    $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 ';
    }
    
    $term = esc_sql( like_escape( $q['s'] ) );
    if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
        $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

    if ( !empty($search) ) {
        $search = " AND ({$search}) ";
        if ( !is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    
    return $search; 
}
add_filter( 'posts_search', '__search_by_title_only', 10, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 */
function __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;    
    $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', '__search_by_title_only', 500, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.

UPDATE: This code has been updated for WordPress 3.2.1. I'm surprised it even worked before as $wpdb wasn't global!

Source Link
TheDeadMedic
  • 36.6k
  • 9
  • 68
  • 102

Here's a filter that'll do the trick. Drop it into your theme's functions.php or a plugin.

/**
 * Search SQL filter for matching against post title only.
 */
function __search_by_title_only( $search, &$wp_query )
{
    if ( empty($search) )
        return $search; // skip processing - no search term in query
    
    $q =& $wp_query->query_vars;

    // wp-includes/query.php line 2128 (version 3.1)
    $n = !empty($q['exact']) ? '' : '%';
    $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 ';
    }
    
    $term = esc_sql( like_escape( $q['s'] ) );
    if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
        $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";

    if ( !empty($search) ) {
        $search = " AND ({$search}) ";
        if ( !is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    
    return $search; 
}
add_filter( 'posts_search', '__search_by_title_only', 10, 2 );

Most of the code is just copied from the WP_Query class, except with the post_content LIKE's removed.