带有自定义分类法的 Wordpress 自定义查询 - 分页错误

Wordpress Custom Query with Custom Taxonomies - Pagination Bug

我创建了一个涉及多个分类的自定义查询,除此之外我还使用了 paginate_links 有分页到位。分类法正在 select 通过使用 onchange="this.form.submit()" 的下拉 select 表单进行编辑,以便能够过滤帖子,这只是一个临时解决方案,但目前工作正常,因此值得注意这种情况下。

分页工作正常,当分类术语 selected 时帖子正确显示,但是当我离开第一页并假设我转到第 2 页并且我 select 不同的分类-term 在下拉 selector 中,URL 保留当前页码,并向其添加 selector ID。而我想要实现的是,当我在 select 中单击不同的分类法时,新的 selected 分类法查询会创建一个新的 URL 从第一页开始某些分类法。

让我举一个例子来更清楚地说明我上面的意思:

  1. 我 select 来自品牌的分类术语“brand1”select或显示与该分类术语相关的所有帖子

URL 在这一点上是 mydomainname.com/?brandselector=brand1,到目前为止一切顺利。

  1. 我通过分页转到第 2 页,现在 URL 看起来像这样:
    mydomainname.com/page/2/?brandselector=brand1

  2. 问题本身从这里开始: 我在任何下拉列表中单击不同的分类术语select或如下图:

现在,如果分类术语没有足够的帖子来达到分页的多个页面,我要么得到一个空页面,要么我从第 2 页或我之前所在的任何其他数字看到帖子列表,新 selected 分类术语。

URL 现在看起来像这样: mydomainname.com/page/2/?brandselector=brand2

因此页码不会在新 select 编辑的分类术语上重置。

我的代码:

    global $wpdb, $post, $page;
    
    echo "<div class='entry-content'>";
    
    echo '<div class="filter-box">';
    
    echo '<form name="selectbrand" method="GET" style="width:200px;">';
    echo '<select id="brandselector" name="brandselector" onchange="this.form.submit()">';
      echo '<option value="">Select brand:</option>';
        $brands = get_terms(array(
                        'taxonomy' => 'brand',
                        'hide_empty' => true,
                        'orderby' => 'name',
                    ) );
       
    foreach ( $brands as $brand ) { ?>
    <?php echo '<option value="' . $brand->slug . '">' . $brand->name . '</option>';
            };
            echo '</select>';
            echo '</form>';
    
    echo '<form name="selectdrive" method="GET" style="width:200px;">';
    echo '<select id="driveselector" name="driveselector" onchange="this.form.submit()">';
      echo '<option value="">Select drive:</option>';
        $drives = get_terms(array(
                        'taxonomy' => 'drive_cat',
                        'hide_empty' => true,
                        'orderby' => 'name',
                    ) );
       
    foreach ( $drives as $drive ) { ?>
    <?php echo '<option value="' . $drive->slug . '">' . $drive->name . '</option>';
            };
            echo '</select>';
            echo '</form>';
            
    echo '</div>'; //filter-box
    
    
    if ( !empty( $_GET['driveselector'])){
        $term_id = $_GET['driveselector'];
        }
    
    
    if ( !empty( $_GET['brandselector'])){
        $term_id = $_GET['brandselector'];
        }
     
     //<-----------------<<<<Main Query start>>>>---------------------
       
       $main_args = array(
                'post_type'=> 'things',
                'orderby' => 'date',
                'order'   => 'DESC',
                'posts_per_page' => 5,
                'paged' => $page,
    );
    
     //<-----------------<<<<Main Query + Custom Query>>>>---------------------
    
    $taxquery = array();
    
    if ( !empty($term_id) || isset($term_id) ) {
        array_push($taxquery,array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'brand',
                'field' => 'slug',
                'terms' => $term_id
                ),
            array(
                'taxonomy' => 'drive_cat',
                'field' => 'slug',
                'terms' => $term_id
                )
            ));
    }
    
    if(!empty($taxquery)){
        $main_args['tax_query'] = $taxquery;
    }
    
    global $main_query;
    $main_query = new WP_Query( $main_args );

'<div class="content-box">';

  if ( $main_query->have_posts() ) :
   while ( $main_query->have_posts() ) : $main_query->the_post();

  //content start here

和分页:

//<!-- end blog posts -->

endwhile;

echo '<div class="pagination">';
$total_pages = $main_query->max_num_pages;
if ($total_pages > 1){
    $current_page = max(1, get_query_var('page'));
       echo paginate_links(array(
            'base' => preg_replace('/\?.*/', '/', get_pagenum_link(1)) . '%_%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
}    
echo '</div>';

wp_reset_postdata();

endif;

单击不同的分类术语时,我缺少什么来重置 URL?任何建议将不胜感激,如果我错误地使用了某些技术术语,我深表歉意,我仍在学习。谢谢。

由于您没有为 form 元素定义 action 属性,默认操作是转到当前 url。因此,一旦您使用分页 hyperlink,下一次表单将向该 link 发送请求。设置动作属性将解决这个问题。

echo '<form action="/" name="selectdrive" method="GET" style="width:200px;">';