Reputation: 752
I've got a function added in functions.php to improve the search functionality in my directory section: http://www.highwaysindustry.com/?post_type=directory_listings&s=king
However, with the following enabled, it overrides the search for the rest of the website also: http://www.highwaysindustry.com
This particular function I would prefer it to be targeted for the directory search only.
Function in functions.php
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 REGEXP '[[:<:]]{$term}[[:>:]]')";
$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', 1000, 2 );
Snippet of my directory-search.php which the form on /Directory uses:
<?php
$args = array(
'post_type'=> 'directory_listings',
's' => $s,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'membership_type',
'orderby' => 'meta_value',
'order' => "DESC"
,
),
),
);
?>
<div class="row">
<div class="small-12 columns">
<div class="directory-banner">
<img src="<?php echo site_url(); ?>/wp-content/themes/highways/images/directory-banner.jpg" alt="" title="" />
<h1><?php echo $s; ?></h1>
</div>
</div>
</div>
**I currently have the function defined above commented out, when enabled it's a lot more specific, for example, searching "King" would only provide back 2 results. Right now the generic search provides too many unspecific results.
Upvotes: 0
Views: 2370
Reputation: 14913
The other 2 answers are also worth investigating. However I'll stick to your solution.
To search only directory_listings post type, tweak the function as shown below
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;
// post_type query var has other posts types as well, return default search
if( ! 'directory_listings' == $q['post_type'] )
return $search
// We are here, it means post_type var contains only directory_listings
// continue with the rest of the code
........
Answering your 2nd query
There is a company called blakedale, if you search just blake you won't get the result. Any method to increase the method of the returned result(s).
The regexp being used in the code below searches for full words hence "blake" doesn't return any results
$search .= "{$searchand}($wpdb->posts.post_title REGEXP '[[:<:]]{$term}[[:>:]]')";
You need to either change the regexp or use SQL LIKE
$search .= "{$searchand}($wpdb->posts.post_title LIKE '%{$term}%')";
P.S: I haven't tested the line of code above, so it may need polishing.
Upvotes: 1
Reputation: 128
You can try this .
$query_args = array( 'post_type' => 'directory_listings', 'tax_query' => array() );
if ( !empty( $search_terms ) ) {
$query_args['s'] = $search_terms;
}
if ( !empty( $search_terms ) ) {
$query_args['tax_query'][] = array(
array(
'taxonomy' => 'directory_listings',
'field' => 'slug',
'terms' => $news_category
)
);
}
$my_posts = new WP_Query( $query_args );
if ( $my_posts->have_posts() ) { // etc
}
Upvotes: 0
Reputation: 5183
did you try using pre_get_posts
?
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search && $_GET['post_type'] == 'directory_listings') {
// add custom meta / tax queries
}
}
}
add_action('pre_get_posts','search_filter');
Upvotes: 0