Get the selected term in a dropdown search

To show dropdown with currently selected option, first you need to fetch current value. You can get that value from $_REQUEST variable. If you have grade variable then your current value is $_REQUEST[‘grade’]. Eg. if ($_REQUEST[‘grade’] == $term->slug) Recommended: Do not use REQUEST variable directly without sanitization. Use function like esc_attr. If you are sure … Read more

WordPress Single Conditional for Search Page

You could hook into the the_permalink filter and check for is_search() to conditionally modify the URLs of your search results to include some additional parameters that your single.php then checks for. Something like this (may require tinkering): add_filter( ‘the_permalink’, ‘wpse155331_the_permalink’ ); function wpse155331_the_permalink( $url ) { if ( is_search() ) { $url = add_query_arg( array( … Read more

How to control WordPress Search Behavior?

What you tried to use is broken. add_filter( ‘pre_get_posts’, ‘modified_pre_get_posts’ ); function modified_pre_get_posts( WP_Query $query ) { if ( $query->is_search() ) { $query->set( ‘post_type’, array( ‘page-home’ ) ); } return $query; } That function definition is not valid. You would have seen an error if you had debugging enabled. You need: add_filter( ‘pre_get_posts’, ‘modified_pre_get_posts’ ); … Read more

Search results URL without query string variables

It’s better to share some of your code that already you used. And you mention first line that you have search page ? what exactly you mean by that ? WordPress have default search page did you mention that one ? if you are then try this code function change_search_url_rewrite() { if ( is_search() && … Read more

search only pages if on page

You can append post_type to the end of your search string like this: http://yourdomain.com/?s=search+string&post_type=page This works like a charm: <form method=”get” action=”<?php echo home_url();?>”> <input type=”text” name=”s” /> <label>Search Pages</label> <input type=”radio” name=”post_type” value=”page” /> <label>Search Posts</label> <input type=”radio” name=”post_type” value=”post” /> <button type=”submit”>Submit</button> </form> You could also do something like this within the form … Read more

Adding arg to search results page

That site you linked to is completely wrong. You should never ever use query_posts to run any type of query. Take your time and read the following post on this matter: When should you use WP_Query vs query_posts() vs get_posts()? You also would not want to run any type of custom query in place of … Read more

search.php template not loading results

Change the form to: <input class=”searchform-mid clearit” type=”text” name=”s” id=”search-header” value=”Search” /> Check that name should be “s” as that the get attribute wordpress use for search keyword

Set minimum number of characters in the search

Quick and dirty, put this before your get_header() in your search.php <?php // Get the query string $query = get_search_query(); // if the first & last char is space, rip them $query = trim($query); // if there are more than one space, rip to one space $query = preg_replace(‘/\s\s+/’, ‘ ‘,$query); // if chars count … Read more

Force WP to use a certain search template

You can use template include filter to use it on every search request. Let’s assume the advanced search page template you are using is ‘wp-advanced-search.php’ you can use locate_template to get its path: add_action(‘template_include’, ‘advanced_search_tmpl’); function advanced_search_tmpl( $template ) { if ( is_search() ) { $t = locate_template(‘wp-advanced-search.php’, false); if ( ! empty($t) ) $template … Read more