WordPress search results
€8-30 EUR
Pagado a la entrega
I need four custom codes for the [login to view URL] files of four different wordpress sites.
The purpose is to limit the wordpress search results.
site 1) Search only at the beginning of the title (of post/page)
site 2) Search only at the end of the title (of post/page)
site 3) Search only within the title, i.e. excluding the first and last letter of the title (of post/page)
site 4) Search anywhere in the title (of post/page) considering the asterisks as unknown letters (for example, if I search for h*ll*, I will find hello)
I already have the following code in [login to view URL] to limit search to post/page titles only and exactly. It works fine.
function ni_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', 'ni_search_by_title_only', 500, 2 );
add_action('pre_get_posts', 'my_make_search_exact', 10);
function my_make_search_exact($query){
if(!is_admin() && $query->is_main_query() && $query->is_search) :
$query->set('exact', true);
endif;
}
Nº del proyecto: #15460194