Display Categories in Search Results

What you basically want to do is to change the SQL query being done to group the result of the query by category The code taken from here groups by performing a sort by category. You can place the following code in your themes search.php file <?php add_filter(‘posts_join’, create_function(‘$a’, ‘global $wpdb; return $a . ” … Read more

I’m trying to expand the scope of the default WP search algorithm. What am I doing wrong?

Add this temporarily to your functions.php. It will break things but you can see the WHERE clause. Be sure to remove it because things won’t work right if you leave it there. function dump_where($w){ var_dump($w); } add_filter(‘posts_where’,’dump_where’); The first thing you should notice is that the % signs get escaped. Remove those and WordPress will … Read more

Customize Search Results for Custom Post Type

According to this answer of yours, you can do the following inside your search.php: if (isset($_GET[‘post_type’])) get_template_part(‘search’, $_GET[‘post_type’]); else // no post_type given Then you have to set up the search-{post_type}.php files. If you want to handle only some of the existing post types differently, do this by means of a switch: $type = (isset($_GET[‘post_type’])) … Read more

Search in post title and custom fields

Here is one idea using the posts_clauses filter: function search_posts_clauses($clauses, $query){ if(!is_admin() && $query->is_main_query() && $query->is_search()){ global $wpdb; // replace post_content with meta_key and meta_value: $searchterms = explode(” “,get_query_var(“s”)); foreach($searchterms as $searchterm){ $from = sprintf(“{$wpdb->posts}.post_content LIKE ‘%%%s%%'”, $searchterm); $to = sprintf(“{$wpdb->postmeta}.meta_key = ‘cities’ AND {$wpdb->postmeta}.meta_value=”%s” “, $searchterm); $clauses[“where”] = str_replace($from, $to, $clauses[“where”]); } $clauses[“join”] = … Read more

get_query_var(‘s’) not working

Probably you need to use the following function somewhere in your template, whether in functions.php or somewhere. function add_query_vars_filter( $vars ){ $vars[] = “s”; return $vars; } add_filter( ‘query_vars’, ‘add_query_vars_filter’ ); It’ll avail your s parameter from the URL into the get_query_var(‘s’).