Reputation: 411
I have a search function that currently searches for products etc using product text and everything. I want it to search for the product titles only. I have used this code and it has worked for years, but doesn't anymore. Now I get an error. Why?
THE CODE USED in functions.php
<?php
// Search titles 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($wpdb->esc_like($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);
THE ERROR
Warning: Parameter 2 to __search_by_title_only() expected to be a reference, value given in /customers/7/a/4/nemoko.dk/httpd.www/wp-includes/class-wp-hook.php on line 286
Upvotes: 1
Views: 7017
Reputation: 973
function __search_by_title_only( $search, &$wp_query )
the second parameter is start with & (&$wp_query) that means it expects the reference variable as parameter.
posts_search
filter passing the WP_Query $this
object. So just remove &
in parameter
Example:
function __search_by_title_only( $search, $wp_query )
Upvotes: 3