phpwordpress

Issues when displaying custom search results in Wordpress


I try to create my first search.php but I have some problems. When I launch a request, input's and filters' content disappear. It seems like my search's filters and input are reset when the result of my request is display.

How can I keep the parameters of my filters and input ?

After that, I also have a problem to display my posts. I have to use global $wp_query; which is not what I saw in tutorials. If I use my variable $query, WordPress' header display the correct search's results but displayed posts doesn't respect my filters by taxonomies.

Is there any problem to use this variable ?

Here is the basic form :

<form method="get" action=" <?php echo home_url('/') ?>">
    <div>
        <input type="search" name="s" />
        <input type="hidden" name="post_type" value="recettes" />
        <input type="submit" value="Rechercher">
    </div>
    <select name="duree">
        <option value="0">Time</option>
        <?php
        $terms = get_terms('time', $filters_args);
        if (!empty($terms) && !is_wp_error($terms)) {
            foreach ($terms as $term) {
        ?>
                <option value="<?php echo $term->slug ?>"><?php echo $term->name ?> </option>
        <?php
            }
        }
        ?>
    </select>
</form>

Here is my request :

$s = get_search_query();
$filters_args = array(
    'orderby' => 'name',
    'order' => 'ASC'
);
$query_args = array(
    'post_type'         => array('recettes', 'post'),
    's'                 => $s,
    'posts_per_page'    => 15
);
$query = new WP_query($query_args);

Here what I use for displaying results :

global $wp_query;
if ($wp_query->have_posts()) {
    while ($wp_query->have_posts()) {
        $wp_query->the_post();
?>
        <h2><?php the_title(); ?></h2>
<?php
    }
    wp_reset_query();
}

Thanks for your help !


Solution

  • Please update your code as per below

    <form method="get" action="<?php echo home_url('/'); ?>">
        <div>
            <input type="search" name="s" value="<?php echo get_search_query(); ?>" />
            <input type="hidden" name="post_type" value="recettes" />
            <input type="submit" value="Rechercher">
        </div>
        <select name="duree">
            <option value="0">Time</option>
            <?php
            $terms = get_terms('time', $filters_args);
            if (!empty($terms) && !is_wp_error($terms)) {
                foreach ($terms as $term) {
                    // Check if the current term is the one selected
                    $selected = (isset($_GET['duree']) && $_GET['duree'] == $term->slug) ? 'selected' : '';
            ?>
                    <option value="<?php echo $term->slug; ?>" <?php echo $selected; ?>>
                        <?php echo $term->name; ?>
                    </option>
            <?php
                }
            }
            ?>
        </select>
    </form>
    

    Here we can check if the taxonomy filter will applied then it will add tax_query

    $filters_args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );
    
    $duree = isset($_GET['duree']) && $_GET['duree'] != '0' ? $_GET['duree'] : '';
    
    $query_args = array(
        'post_type'         => array('recettes', 'post'),
        's'                 => $s,
        'posts_per_page'    => 15,
    );
    
    if ($duree) {
        $query_args['tax_query'] = array(
            array(
                'taxonomy' => 'time',
                'field'    => 'slug',
                'terms'    => $duree,
            )
        );
    }
    $query = new WP_Query($query_args);
    
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();
            ?>
            <h2><?php the_title(); ?></h2>
            <?php
        }
        // Reset post data
        wp_reset_postdata();
    } else {
        echo 'No posts found';
    }