Created
April 22, 2015 13:18
-
-
Save mafsdisseny/1adcae6295710e7ea609 to your computer and use it in GitHub Desktop.
Limit WordPress search to post titles, apply site-wide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Buscar solo coincidencias en el titulo | |
// Source: http://www.paulund.co.uk/limit-wordpress-search-to-post-titles | |
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment