0

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>
      </select>
<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:

?s=Barcelona&cat=7

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:

?s=cat=7

And with this all the post that contains Barcelona.

?s=Barcelona

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

0

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;
    $wp->add_query_var('cat');

}

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 Nov 28 '16 at 10:36
  • I have edited my code please check and try @MidouCloud – Govind Kumar Nov 28 '16 at 10:43
  • That didnt work but i find a solution editing the query in the template file search.php – MidouCloud Nov 29 '16 at 9:48
  • Great ! @MidouCloud – Govind Kumar Nov 29 '16 at 9:52
0

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, privacy policy and cookie policy

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