2

I'm trying to create a custom taxonomy.php page where my users can refine search into that context. I've tried to create a kind of loop like:

<?php 
                if(!empty($_GET['ending']))
                {
                    $ends = array(
                        'key' => 'ending',
                        'value' => (current_time('timestamp', 0) + 3600*24*$_GET['ending']),
                        //'type' => 'numeric',
                        'compare' => '<'
                    );
                }
                if(!empty($_GET['budgets']))
                {
                    $budgets = array(
                        'key' => 'budgets',
                        'value' => $_GET['budgets'],
                        //'type' => 'numeric',
                        'compare' => '='
                    );
                }
                $taxonomy = get_query_var('taxonomy');
                $post_per_page = 10;
                $args = array(
                    'post_type' => 'project',
                    'order' => 'DESC', 
                    'orderby' => 'date', 
                    'posts_per_page' => $post_per_page,
                    'paged' => 1, 
                    'meta_query' => array($closed, $ends, $budgets ),
                    's'=> GET('keyword'),
                    'post_status' =>array('publish'),
                    'tax_query' => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field' => 'slug',
                        'terms' => $term->slug
                        )
                    )
                );
                query_posts($args);

                if ( have_posts() ): while ( have_posts() ) : the_post();
                    KleeiaDev_get_post(); 
                endwhile; 

                if(function_exists('wp_pagenavi')):
                    wp_pagenavi(); endif;

                wp_reset_postdata();

                else:
                    echo __('No projects posted here yet.',"KleeiaDev");
                endif;

                wp_reset_query();
                ?>

My elements that should filter the query are $ends, $budgets and 's'=> GET('keyword') which are connected to my side form like the following.

<form method="get" action="<?php echo $lnk ?>">
                        <div class="col-sm-12">
                            <input type="text" placeholder="<?php _e('Type something here','KleeiaDev') ?>" value="<?php echo stripslashes($_GET['keyword']) ?>" name="keyword" size="30" class="form-control" />
                        </div>
                        <div class="col-sm-6 col-md-12">
                            <?php echo ProjecTheme_get_budgets_dropdown($_GET['budgets'], 'form-control top-15' , 1); ?>
                        </div>
                        <div class="col-sm-6 col-md-12">
                            <select name="ending" class="form-control top-15">
                                <option value=""><?php _e('Ending in','KleeiaDev') ?></option>
                                <option value="1" <?php echo ($_GET['ending'] == 1 ? 'selected="selected"' : '') ?>><?php _e('1 day','KleeiaDev') ?></option>
                                <option value="7" <?php echo ($_GET['ending'] == 7 ? 'selected="selected"' : '') ?>><?php _e('7 days','KleeiaDev') ?></option>
                                <option value="30" <?php echo ($_GET['ending'] == 30 ? 'selected="selected"' : '') ?>><?php _e('30 days','KleeiaDev') ?></option>
                                <option value="180" <?php echo ($_GET['ending'] == 180 ? 'selected="selected"' : '') ?>><?php _e('6 Months','KleeiaDev') ?></option>
                            </select>
                        </div>
                        <div class="col-sm-6 col-md-12">
                            <input type="submit" name="apply_filters" class="submit_green top-15 full_width text-center" value="<?php _e('Refine search','KleeiaDev') ?>" />
                        </div>
                    </form>

My goal is to refine results with 2 fixed conditions: $taxonomy && $closed [all posts in $taxonomy which are $closed] + eventually [$budgets && keyword && $ends]. When I submit, the form produces a url like mydomain.com/?keyword=hello&budgets=&ending=7&apply_filters=Refine+search but I get always all results in that taxonomy without filters applied. I don't know how to move on because I'm not very expert of queries. I've read the query_posts codex section without going further though.

  • Can somebody drive me through something? – huraji May 29 '16 at 11:58
1

After some tweaking I've discovered that I've totally forgotten the relation into meta_query. so I've just added

'meta_query' => array(
                    'relation' => 'AND',
                    $closed, 
                    $ends, 
                    $budgets, 
                ),

and it worked fine.

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.