Maybe someone can help me identify a problem I am experiencing with the pre_get_posts action.
I have used the same code in another project before and it worked fine. I even went through line by line but may I am missing something. I am not sure what it is, here is the code and my explanation is below.
archive-specials.php
<form role="form" method="POST" action="<?php echo site_url('specials'); ?>">
<?php echo wp_nonce_field('search_specials', 'search_specials_nonce', false, false); ?>
<div class="form-group">
<label for="special_day">Select Day:</label>
<?php wp_dropdown_categories(array(
'show_option_all' => 'Everyday',
'show_count' => 1, //default is 0
'hide_empty' => false,
'name' => 'special_day',
'id' => 'special_day',
'class' => 'form-control',
'hierarchical' => true,
'taxonomy' => 'special_day'
)); ?>
</div>
<div class="form-group">
<label for="special_area">Select Area:</label>
<?php wp_dropdown_categories(array(
'show_option_all' => 'All Areas',
'show_count' => 1, //default is 0
'hide_empty' => false,
'name' => 'special_area',
'id' => 'special_area',
'class' => 'form-control',
'hierarchical' => true,
'pad_counts' => true, //add children count to curent item,
'taxonomy' => 'special_area'
)); ?>
</div>
<div class="form-group">
<label for="special_category">Select Category:</label>
<?php wp_dropdown_categories(array(
'show_option_all' => 'All Categories',
'show_count' => 1, //default is 0
'hide_empty' => false,
'name' => 'special_category',
'id' => 'special_category',
'class' => 'form-control',
'hierarchical' => true,
'pad_counts' => true, //add children count to curent item,
'taxonomy' => 'special_category'
)); ?>
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
functions.php
add_action('pre_get_posts', function($query) {
$search_specials_nonce = filter_input(INPUT_POST, 'search_specials_nonce');
if(isset($search_specials_nonce) && wp_verify_nonce($search_specials_nonce, 'search_specials') && $query->is_main_query()) {
$special_day = filter_input(INPUT_POST, 'special_day');
$special_area = filter_input(INPUT_POST, 'special_area');
$special_category = filter_input(INPUT_POST, 'special_category');
$tax_query = array();
if($special_day) {
$tax_query = array_merge($tax_query, array(
array(
'taxonomy' => 'special_day',
'field' => 'term_id',
'terms' => $special_day,
'include_children' => true
)
));
}
if($special_area) {
$tax_query = array_merge($tax_query, array(
array(
'taxonomy' => 'special_area',
'field' => 'term_id',
'terms' => $special_area,
'include_children' => true
)
));
}
if($special_category) {
$tax_query = array_merge($tax_query, array(
array(
'taxonomy' => 'special_category',
'field' => 'term_id',
'terms' => $special_category,
'include_children' => true
)
));
}
if(count($tax_query) > 1) {
$tax_query = array_merge($tax_query, array(
'relation' => 'AND'
));
}
if(count($tax_query) > 0) {
$query->set('tax_query', $tax_query);
}
}
});
The problem I am experiencing is that no records shows up in the main loop after submitting the search form. When I load the specials archive page it loads the records but not after the search is done. I have tried many things and even commenting stuff out but nothing seems to work.
The nonce definitely verifies correctly and the query is only affecting the main query but no results are showing up. I use the regular way of showing results in the archive-specials.php page.
while(have_posts()) { the_post();
echo the_title();
}
Any help or suggestions would be much appreciated.
WHERE 1=1 AND 0 = 1
is an impossible condition, something in your tax query is invalid.