• Hi,

    I edit my wordpress page searchform.php in theme to add filter by subcategories. And i add a add_filter(‘the_search_query’,’search_filter’) in my function.php

    But when run a search : result dont care about my dropdown…

    ==> My question : How to make search (filter) works with my catégory filter ?

    Hi have this :
    https://i.stack.imgur.com/x0kEM.jpg

    This in my searchpage.php :

    <?php
    $options_type = array(
     'name'               => 'subcat_type',
     'hierarchical'       => 1,
     'parent'             => get_category_by_slug('action-type')->term_id,
     'show_option_none'   => ("Type d’action"),
     'selected'           => (isset($_GET['subcat_type']) ? $_GET['subcat_type'] : ''),
     'hide_empty'   => 0  ); 
    
    .......
    
    ?>
    
    <form method="get" id="search-form" action="<?php echo home_url(); ?>/">
        <input class="search-box vntd-boxed-content" name="s" id="s" type="text" value="<?php echo isset($_GET['s']) ? $_GET['s'] : '' ?>" placeholder="<?php _e('Que recherchez vous ?','vntd_renown') ?>">
        <?php wp_dropdown_categories($options_type); ?>
        <?php wp_dropdown_categories($options_thematiques); ?>
        <?php wp_dropdown_categories($options_public); ?>
        <?php wp_dropdown_categories($options_territoire); ?>
        <input type="hidden" id="my_search" name="my_search" value="c_search" />
        <input type="submit" id="searchsubmit" value="Rechercher" />
    </form>

    this in my functions.php :

    <?php
    // Define search filter
    function search_filter( $query ) {
        // only modify your custom search query.
        if ( $query->is_search &&  $_post['my_search'] == "c_search") {
            $args = array(
                    'relation' => 'AND',
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['subcat_public']),
                    'operator' => 'IN'
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['subcat_territoire']),
                    'operator' => 'IN'
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['subcat_thematiques']),
                    'operator' => 'IN'
                ),
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => array( $_post['subcat_type']),
                    'operator' => 'IN'
                )
            );
            $query->set( 'tax_query', $args);
        }
        return $query;
    }
    add_filter( 'the_search_query','search_filter');
    ?>
  • The topic ‘Why my custom sub-category SEARCH FILTER don't work ?’ is closed to new replies.