0

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.

6
  • var_dump $wp_query in the template and look at the query your filter code generates. It should be apparent why it's not returning anything.
    – Milo
    Nov 28 '14 at 13:40
  • Thank you Milo. I have done that and definitely see [post_count] => 0 but am battling to understand why. I am using the exact same code and approach in another project and it works well. Nov 28 '14 at 13:54
  • what's the SQL query?
    – Milo
    Nov 28 '14 at 14:00
  • SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND 0 = 1 AND wp_posts.post_type = 'specials' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3 Nov 28 '14 at 14:02
  • WHERE 1=1 AND 0 = 1 is an impossible condition, something in your tax query is invalid.
    – Milo
    Nov 28 '14 at 14:04
0

The issue I had with my search was that the select field's name was the same name as the taxonomy being queried. When I used a different name for the select field my query worked as it should.

<?php wp_dropdown_categories(array(
    'name' => 'special_day_select', //anything else besides an existing taxonomy name.
    'taxonomy' => 'special_day'
)); ?>

I hope this is useful for someone else out there.

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.