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

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’).

Refine Search Results

Search is a big topic and therefore I can only guess the direction you want to go to. Like @MikeSchinkel in his comment was asking for more, I can for the moment only provide a list of links of which one might come close to what you need: Refine search by category (How-To); Building a … Read more

Search form with Category and Sub Category

First you have to give your dropdown names so: <?php $media = array( ‘name’ => ‘subcat’, ‘hierarchical’ => 1, ‘parent’ => get_cat_id(‘Media’), ‘show_option_none’ => (‘All Media’), ‘hide_empty’ => 0 ); ?> <form method=”get” id=”searchform” action=”<?php bloginfo(‘url’); ?>/”> <div> <input type=”text” value=”<?php the_search_query(); ?>” name=”s” id=”s” /> <?php wp_dropdown_categories(‘name=maincat&show_option_none=All Category’); ?> <?php wp_dropdown_categories($media); ?> <input type=”hidden” … Read more

Do not allow to search certain words

Try this in your functions.php, and change news for whatever you want to be blocked in your site. add_action(‘wp’, ‘check_search’); function check_search() { global $wp_query; if (!$s = get_search_query()) return false; if (preg_match(‘/news/’, $s)) { $wp_query->set_404(); status_header(404); get_template_part(404); exit(); } } Hope it helps.

Which template holds what to display when search returns nothing

get_template_part( ‘content’, ‘none’ ); will look for: content-none.php content.php Twenty Thirteen does have content-none.php. In general this is organized in such way to support dynamic lookups where first two-part template might or might not exist and fallback to more generic template, if necessary.