1

I created a search form which searching by category filter and keyword input. The search form code is here-

<form action="<?php bloginfo('url'); ?>" method="get" role="search" class="dropdown-form">
                <div class="input-group">
                        <span class="input-group-addon">
                            <?php
                            wp_dropdown_categories(array(
                                'show_option_all'   => 'all categories',
                                'class'             => 'search_cats'
                            ));
                            ?>
                        </span>
                    <input type="search" class="form-control" placeholder="<?php esc_html_e('Search anything...', 'onepro'); ?>" name="s">
                    <span class="input-group-addon">
                        <button type="submit"><i class="ion-android-arrow-forward"></i></button>
                    </span>
                </div>
            </form>

Then I added the pre_get_posts hook to bottom of the functions.php file-

add_action('pre_get_posts', function() {
  global $wp_query;
  if (is_search()) {
    $cat = intval($_GET['cat']);
    $cat = ($cat > 0) ? $cat : '';
    $wp_query->query_vars['cat'] = $cat;
  }
});

The search form is working as well. But the bellow notice is appearing on where I used WP_Query() to display the post categories -

Notice: is_search was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in C:\xampp\htdocs\onepro\wp-includes\functions.php on line 3996

Here is the query code-

    global $wp_query;
    global $paged;
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query(array(
        'post_type'         => 'post',
        'posts_per_page'    => $atts['show_posts'],
        'paged'             => $paged,
    ));

    if ( $wp_query->have_posts() ) :

        $all_cat_slug = array();
        while ( $wp_query->have_posts() ) : $wp_query->the_post();
            $category = get_the_category();
            foreach( $category as $cat ){
                array_push($all_cat_slug, $cat->slug);
            }
        endwhile;
        $all_cat_slug = array_unique( $all_cat_slug );

    endif;

        <!--Portfolio Filter-->
        <div class="row filters_row text-left">
            <ul class="nav navbar-nav" id="blogs_filters">
                <li data-filter="*" class="active"><?php echo esc_html__('all', 'onepro-essential'); ?></li>
                <?php
                foreach( $all_cat_slug as $cs ){
                    $catname = get_category_by_slug( $cs );
                    echo '<li data-filter=".category-'. $cs .'">'. $catname->name .'</li>';
                }
                ?>
            </ul>
        </div>

The error notice occurs before the categories shown The error screenshot- enter image description here How can I fix this issue?

  • I'm tested your code and I don't get the warning reported. Is there any other code involved? – Dave Romsey Oct 17 '16 at 6:23
  • 1
    what does it mean exactly "where I used WP_Query()"? – Mark Kaplun Oct 17 '16 at 6:32
  • @DaveRomsey I updated the question with other code that is involved with the error. – Eh Jewel Oct 17 '16 at 7:12
  • @MarkKaplun I've just updated the question with the query code. The error occurs before the categories shown. – Eh Jewel Oct 17 '16 at 7:20
3

The Why part

In the core is_search() function there's a check if the global $wp_query is set:

global $wp_query;

if ( ! isset( $wp_query ) ) {
    _doing_it_wrong( __FUNCTION__, 
       __( 'Conditional query tags do not work before the query is run.
            Before then, they always return false.' ), '3.1.0' );
    return false;
}

Note that you're unsetting it with:

$wp_query = null;

just before you create a new WP_Query subquery, that calls is_search() when pre_get_posts fires.

That's when the _doing_it_wrong() is activated.

Workaround

Always try to use the main query, instead of extra WP_Query sub-queries if possible, to avoid running extra database queries.

To target the main search query in the front-end, we can use:

add_action('pre_get_posts', function( \WP_Query $q ) {
    if ( 
            ! is_admin()         // Only target the front-end
         && $q->is_main_query()  // Target the main query
         && $q->is_search()      // Target a search query
    ) {
        // do stuff
    }
});
0

Thanks @birgire. Got a similar issue here with the following error message and got it fixed up by commenting out the line : $wp_query = null;

Error message:

Notice : is_home was called incorrectly . Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in (MYsyspath)/functions.php on line 4146

And it worked, the culprit was the global variable unset action on my index.php

$wp_query = null; to //$wp_query = null;

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.