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-
How can I fix this issue?