主查询按类别扰乱Pre_Get_POST查询

时间:2021-11-16 作者:Joan Chaparro

我有一个按自定义字段排序的自定义帖子的存档页面。我有两个按自定义类别应用过滤器的按钮。结果不符合预期。结果数量正确,但帖子内容不匹配。

这是我的pre\\u get\\u post in函数。php

function xgc_filter_archive( $query ) {
    if ($query->is_main_query()){
        if ( is_admin() ) {
                return;
        }
        if ( is_post_type_archive (\'concerts\') ) {
                if ( \'cat\' === $_GET[\'getby\']  ) {
                            $taxquery = array(
                                    array(
                                            \'taxonomy\' => \'categories_concerts\',
                                            \'field\' => \'slug\',
                                            \'terms\' => $_GET[\'cat\'],
                                    ),
                            );
                            $query->set( \'tax_query\', $taxquery );
                }
        }
        if ( is_post_type_archive (\'obres\') ) {
                if ( \'cat\' === $_GET[\'getby\']  ) {
                        $taxquery = array(
                                array(
                                        \'taxonomy\' => \'categories_obres\',
                                        \'field\' => \'slug\',
                                        \'terms\' => $_GET[\'cat\'],
                                ),
                        )
                        ;
                        $query->set( \'tax_query\', $taxquery );
            }
        }
        return $query;
    }
}
add_action( \'pre_get_posts\', \'xgc_filter_archive\');
这是我的模板:

<section id="menu_categories_obres" class="py-2">
     <div class="filter-custom-taxonomy container justify-content-center">
      <?php
        $terms = get_terms( \'categories_obres\' );
        foreach ( $terms as $term ) : ?>
          <a href="?getby=cat&cat=<?php echo esc_attr( $term->slug ); ?>" class="btn btn-md btn-primary mt-2" tabindex="-1" role="button" aria-disabled="true"><?php echo esc_html( $term->name ); ?>
          </a>
      <?php endforeach; ?>
    </div>
</section>
<section id="llistat_obres" class="py-3">
  <div class="container" style="display: flex;flex-wrap: wrap;">
  <?php 
                // Variables
                // query
                $posts = get_posts(array(
                        \'post_type\'         => \'obres\',
                        \'posts_per_page\'    => -1,
                        \'meta_key\'          => \'any\',
                        \'orderby\'           => \'meta_value\',
                        \'order\'             => \'DESC\'
                         ));
                if( $posts ): 
                    while ( have_posts() ) : the_post();
如果我对主查询进行注释,效果很好,但我并没有按需要进行排序。

感谢您的回复

EDIT:

现在,如果我把这个代码放在函数上。php

function xgc_filter_archive( $query ) {
    if ($query->is_main_query()){
        if ( is_admin() ) {
                return;
        }
        if ( is_post_type_archive (\'concerts\') ) {
                if ( \'cat\' === $_GET[\'getby\']  ) {
                            $taxquery = array(
                                    array(
                                            \'taxonomy\' => \'categories_concerts\',
                                            \'field\' => \'slug\',
                                            \'terms\' => $_GET[\'cat\'],
                                    ),
                            );
                            $query->set( \'tax_query\', $taxquery );
                            $query->set( \'post_type\', \'concerts\'  );
                            $query->set( \'posts_per_page\', -1 );
                            $query->set( \'meta_key\', \'data_del_concert\');
                            $query->set( \'orderby\', \'meta_value\' );
                            $query->set( \'order\', \'DESC\' );
                }
        }
        if ( is_post_type_archive (\'obres\') ) {
            if ( \'cat\' === $_GET[\'getby\']  ) {
                        $taxquery = array(
                                array(
                                        \'taxonomy\' => \'categories_obres\',
                                        \'field\' => \'slug\',
                                        \'terms\' => $_GET[\'cat\'],
                                ),
                        )
                        ;
                        $query->set( \'tax_query\', $taxquery );
                        $query->set( \'post_type\', \'obres\'  );
                        $query->set( \'posts_per_page\', -1 );
                        $query->set( \'meta_key\', \'any\');
                        $query->set( \'orderby\', \'meta_value\' );
                        $query->set( \'order\', \'DESC\' );
            }
        }
            return $query;
    }
}
add_action( \'pre_get_posts\', \'xgc_filter_archive\');
如果我点击过滤器按钮,效果很好,而且顺序也很好,但一开始所有帖子的顺序都是错误的。此代码运行不正常。有什么建议吗?

$posts_list = get_posts(array(
                    \'post_type\'         => \'obres\',
                    \'posts_per_page\'    => -1,
                    \'meta_key\'          => \'any\',
                    \'orderby\'  => \'meta_value\',
                    \'order\'             => \'DESC\',
                     ));
            if( $posts_list ): 
                while ( have_posts() ) : the_post();
谢谢

1 个回复
SO网友:BlueSuiter

在以下位置使用不同的变量名$posts. 您不应该使用任何全局变量名来避免此类事件。

另外,请致电wp_reset_query() 获取数据后。这将把查询重置为默认值。

有关全局变量列表,请参阅link.

编辑查询,如下所示:

$posts_list = get_posts(array(
        \'post_type\'         => \'obres\',
        \'posts_per_page\'    => -1,
        \'meta_key\'          => \'any\',
        \'orderby\'           => \'meta_value_num\',
        \'order\'             => \'DESC\',));
if( $posts_list ): 
    while ( have_posts() ) : the_post();

相关推荐