'Custom Filter loosing the category selection on page 2 - Wordpress
We have created a custom filter to filter the posts from CPT in WordPress.
When we query the posts from a particular category works great, but when we go to the next page of posts with same category, it start displaying the posts from all the categories instead of holding that selected category in the filter.
Tried different query terms but had no luck, any help would be appreciated.
Below is query we used
<div class="select_wrapper" style="background: #fff; ">
<form method="post" action="<?php the_permalink($this_page_id); ?>">
<select id="the_services_location" name="categories" onchange="this.form.submit();">
<option value="">Category</option>
<option class="" value="">Show All</option>
<?php $the_categories = '';
$terms = get_terms(
array(
'taxonomy' => 'bulletin_category',
'hide_empty' => true,
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$the_categories = '<option class="'.$term->slug.'" name="'.$term->slug.'" value="'.$term->slug.'">'.$term->name.'</option>'.$the_categories;
}
}
echo $the_categories;
?>
</select>
</form>
</div>
Solution 1:[1]
Thank you for looking into it, I have found a solution from one of the other threads.
I changed the form method to GET instead of the POST which fixed the issue.
If someone's having same kind of issue.
<div class="select_wrapper" style="background: #fff; ">
<form method="get" action="<?php the_permalink($this_page_id); ?>">
<select id="the_services_location" name="categories" onchange="this.form.submit();">
<option value="">Category</option>
<option class="" value="">Show All</option>
<?php $the_categories = '';
$terms = get_terms(
array(
'taxonomy' => 'bulletin_category',
'hide_empty' => true,
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$the_categories = '<option class="'.$term->slug.'" name="'.$term->slug.'" value="'.$term->slug.'">'.$term->name.'</option>'.$the_categories;
}
}
echo $the_categories;
?>
</select>
</form>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2404814 |
