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

问题描述

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

Pagination works fine,posts are being displayed correctly when taxonomy terms selected,however when I leave the first page and let's say I go to page #2 and I select a different taxonomy-term in the drop-down selector,the URL保留当前页码,并向其添加选择器 ID。而我想要实现的是当我在选择器中单击不同的分类法时,新选择的分类查询会创建一个新 URL,从该特定分类法的第一页开始。

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

  1. 我从品牌选择器中选择分类术语“brand1”以显示与该分类术语相关的所有帖子

enter image description here

此时的 URL 是 mydomainname.com/?brandselector=brand1,到目前为止一切都很好。

  1. 我通过分页转到第 2 页,现在 URL 如下所示:
    mydomainname.com/page/2/?brandselector=brand1

  2. 问题本身从这里开始:我在任何下拉选择器中单击不同的分类术语,如下所示:

enter image description here

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

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

因此页码不会在新选择的分类术语上重置。

我的代码

    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',) );
       
    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','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?任何建议将不胜感激,如果我错误地使用了某些技术术语,我很抱歉,我仍在学习。谢谢。

解决方法

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

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