I've created a custom query with multiple taxonomies involved, and on top of that I use paginate_links
to have pagination in place. Taxonomies are being selected by drop-down select forms using onchange="this.form.submit()"
to be able to filter posts and which is only a temporary solution but works fine for now, and worth noting for the sake of this case.
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 keeps the current page number, and adds the selector ID to it. Whereas what I would like to achive is when I click on a different taxonomy in the selector, the newly selected taxonomy query creates a new URL to start from the first page of that certain taxonomy.
Let me bring up an example to be more clear of what I mean above:
- I select taxonomy term "brand1" from the brand selector to display all posts related to that taxonomy term
URL at this point is mydomainname.com/?brandselector=brand1
and so far so good.
-
I go to page number #2 via the pagination, now the URL looks like this:
mydomainname.com/page/2/?brandselector=brand1
-
The issue itself starts here: I click on a different taxonomy-term in any of the drop down selectors as shown below:
Now I either get an empty page if the taxonomy-term doesn't have enough posts to reach multiple pages of pagination, or I see lists of posts right from page 2 or whichever else number I was on before, of the newly selected taxonomy term.
URL now looks like this: mydomainname.com/page/2/?brandselector=brand2
So the page number is not being reset on a newly selected taxonomy-term.
My code:
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
And the pagination:
//<!-- 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;
What do I missing to reset URL when clicked on a different taxonomy-term? Any suggestions would be highly appreciated, and sorry if I used certain technical terms wrong, I'm still learning. Thanks.
https://stackoverflow.com/questions/67360188/wordpress-custom-query-with-custom-taxonomies-pagination-bug May 03, 2021 at 03:05AM
没有评论:
发表评论