I want to put a filter system in my website that contain a search input and list of states, if I choose a state without writing in input it'll give me all posts in that state and if I search for somthing and specifying a state it'll give me what I searched for in the selected state.

the first part work very well but the second (both search and select state) is not working. Am I wrong with my logic in the query ??

my html form:

<div class="filter" data-url="<?php echo $taxonomy_name . '/' .$term_slug. '/' ; ?>" data-filter="<?php echo 'wilaya'; ?>">
    <input type="text" name="s" class="form-control" placeholder="Rechercher">

    <select class="form-control">
        <?php if(!$_GET['wilaya']) : ?>
        <option value="0">Choix</option>
        <?php else : ?>
        <option value="<?php echo $field['value'][0] ?>"><?php echo $field['choices'][$field['value'][0]] ?></option>
        <?php endif; ?>
        <?php foreach ($field['choices'] as $choice_value => $choice_label) : ?>
            <option value="<?php echo $choice_value ?>"> <?php echo $choice_label ?></option>
        <?php endforeach; ?>

    <button class="btn btn-default" id="filter-btn">Filtrer</button>


And my PHP code is (both search and select):

(the tax_query part is responsible of selected state with custom taxonomy)

$term_slug = get_query_var( 'term' );
$taxonomy_name = get_query_var( 'taxonomy' );

$args = array(
        'post_type' => 'centers',
        'orderby' => 'post_id',
        'meta_key'      => 'center_state',
        'meta_value'    => $_GET['state'],
        'order' => 'DESC',
        'meta_query' => array(
                    'key' => 'center_title',
                    'value' => $_GET['d'],
                    'compare' => 'LIKE'
        'tax_query' => array(
                'taxonomy' => $taxonomy_name,
                'field'    => 'slug',
                'terms'    => $term_slug,

And sorry for my english :)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.