I made multiple custom search on my wordpress site to find post with category search. Like this:

    <select id="category-select" name="category-select">
       <option value="6">Todo</option>
       <option value="7">Spain</option>
       <option value="8">Europe</option>
       <option value="30">Rest of the World</option>
<input type="text" id="autoc-input" name="autoc-input" autocomplete="off" placeholder="">

My form sends correctly all the parameters, for example if i search Barcelona for the category Spain i got this url:


I can't get any result from this query, but if i search just for my input or category. With this i got all the post from the category on the search page:


And with this all the post that contains Barcelona.


I can't find the issue to get the results with more than one parameter. How can i fix this?

2 Answers 2


You can search post by modify the query using pre_get_post filter. add this code inside your functions.php file

add_filter('pre_get_posts', 'search_by_category');
function search_by_category($wp_query)
    if (isset($_GET['cat']) && !is_admin()) {
        $wp_query->set('category__in', array($_GET['cat']));

Before using any query variable you must have to register/add using add_query_var.

function add_category_query_string() {

    global $wp;


add_filter('init', 'add_category_query_string');

let me know if it works for you .

  • I still have the same issue. Wordpress already search by category but when i add two parameters i dont get any result. I made my custom search template (outside from wordpress search) and everything works fine but that dont fix wordpress search page
    – MidouCloud
    Commented Nov 28, 2016 at 10:36
  • I have edited my code please check and try @MidouCloud
    – GKS
    Commented Nov 28, 2016 at 10:43
  • That didnt work but i find a solution editing the query in the template file search.php
    – MidouCloud
    Commented Nov 29, 2016 at 9:48
  • Great ! @MidouCloud
    – GKS
    Commented Nov 29, 2016 at 9:52

Wordpress search is very incomplete to do good post search. I find an answer editing search.php on the top of my template file.

$s = $_GET['s'];
$cat = $_GET['cat'];
$custom_query_args = array(
                        's' => $s,
                        'cat' => $cat,
                        'posts_per_page'   => 11,
                        'order'            => 'DESC',
                        'post_type'        => 'post',
                        'post_status'      => 'publish'

// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );

// Pagination fix
$temp_query = $wp_query;
$wp_query   = NULL;
$wp_query   = $custom_query;

Like this i got all the results of the selected category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.