错误地调用了IS_Search

时间:2016-10-17 作者:Eh Jewel

我创建了一个搜索表单,通过类别过滤器和关键字输入进行搜索。搜索表单代码在此处-

<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>
然后我添加了pre_get_posts 钩住底部functions.php 文件-

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;
  }
});
搜索表单也在运行。但下面的告示出现在我使用的地方WP_Query() 显示职位类别的步骤-

注意:is\\U search调用不正确。运行查询之前,条件查询标记不起作用。在此之前,它们总是返回false。有关详细信息,请参阅WordPress中的调试。(此消息是在版本3.1.0中添加的。)在C:\\xampp\\htdocs\\onepro\\wp中包含\\个函数。php在线3996

以下是查询代码-

    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>
错误通知出现在显示错误屏幕截图的类别之前-enter image description here 如何解决此问题?

2 个回复
SO网友:birgire

核心部分的原因is_search() 函数检查全局$wp_query 已设置:

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;
}
请注意,您正在使用以下方法来解决此问题:

$wp_query = null;
在创建新WP_Query 子查询,调用is_search() 什么时候pre_get_posts 火灾。

那时_doing_it_wrong() 已激活。

解决方法

始终尝试使用主查询,而不是额外查询WP_Query 子查询(如果可能),以避免运行额外的数据库查询。

要以前端的主搜索查询为目标,我们可以使用:

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
    }
});

SO网友:Aness

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;

相关推荐

使用WP-CLI和/或Search-Replace-DB迁移多站点

我有一个Wordpress 5.5/PHP7.3多站点生产站点安装,我正在尝试将其迁移到本地LAMP开发环境,以便进行编辑。The Problem: 就是在运行Search Replace DB脚本或WP Cli多站点迁移脚本时。一个子网站完美地加载到新的开发域上,同时主网站和具有相同主题的子网站成功加载,但主题似乎已中断(基于visual composer)。可能还有其他非视觉错误。我已经下载了所有文件,正确配置了LAMP virtualhost,并通过PHPMYADMIN将数据库导入到我的本地LAMP