<? get_header() ;?>
<?
$cat_param = 'select-category';
if (strpos($_SERVER['REQUEST_URI'], $cat_param)) {
$category_id = htmlspecialchars($_GET[$cat_param]);
} else {
$category_id = null;
}
?>
<article id="post-<? the_ID(); ?>" <? post_class('bg-white'); ?> data-file="<? echo basename(__FILE__); ?>">
<div class="wrap-outer">
<div class="py-5 py-md-6 py-lg-7 py-xl-8 wrap-inner">
<div class="container-fluid container-lg">
<div class="row">
<div class="col-12 col-md-8">
<form method="" action="<? echo get_post_type_archive_link('pcm_review'); ?>/">
<div class="form-group posts-filter">
<div class="input-group">
<div class="input-group-prepend">
<label class="input-group-text" for="review-select-category"><i class="fas fa-filter"></i></label>
</div>
<?
$args_cats = array(
'show_option_all' => 'Show All',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 0,
'echo' => 1,
'selected' => $category_id,
'name' => $cat_param,
'id' => $cat_param,
'class' => 'select-category form-control ml-auto',
'taxonomy' => 'category',
'hide_if_empty' => false,
'option_none_value' => -1,
'value_field' => 'term_id',
'required' => false,
);
wp_dropdown_categories( $args_cats );
?>
</div>
</div>
<script type="text/javascript">
jQuery(function() {
$('#select-category').change(function() {
this.form.submit();
});
});
</script>
<hr>
</form>
<?
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_reviews = array(
'post_type' => 'pcm_review',
'cat' => $category_id,
'paged' => $paged,
);
$query_reviews = new WP_Query( $args_reviews );
if ( $query_reviews->have_posts() ) {
while ( $query_reviews->have_posts() ) {
$query_reviews->the_post();
$content = get_post_meta(get_the_ID(), 'pcm_review_content', true);
$name = get_post_meta(get_the_ID(), 'pcm_review_name', true);
$rating_str = get_post_meta(get_the_ID(), 'pcm_review_rating', true);
$rating_int = intval($rating_str);
$rating_star = '<i class="fas fa-star mr-1 text-warning"></i>';
$rating_stars = str_repeat($rating_star, $rating_int);
?>
<div id="post-<? the_ID(); ?>" <? post_class(''); ?>>
<p>
<span class="review-stars"><? echo $rating_stars; ?></span><br>
<b><? the_title(); ?></b>
</p>
<div class="review-content">
<? echo $content; ?>
</div>
<div class="justify-content-between row small text-muted">
<div class="col-auto">
<i class="fas fa-user mr-2"></i><span class="review-name"><? echo $name; ?></span>
</div>
<div class="col-auto">
<i class="fas fa-tag mr-2"></i><span class="review-categories"><? echo get_the_category_list(', '); ?></span>
</div>
</div>
<hr>
</div>
<?
}
the_posts_pagination();
} else {
pcm_no_results();
}
wp_reset_postdata();
?>
</div>
<div class="col-12 col-md-4">
<div class="sidebar-wrap sticky-top">
<? get_template_part('template-parts/aside-blog'); ?>
<? get_template_part('template-parts/aside-reviews'); ?>
</div>
</div>
</div>
</div>
</div>
</div>
</article>
<? get_footer() ;?>
Struggling with a pagination issue and would really appreciate any help. I’m not making any progress in searching here or other forums so thought I’d ask for help.
I have a custom post type for testimonials/reviews which supports the category taxonomy. My template file includes a dropdown/select-list to allow the visitor to choose a category if they’d like to filter the reviews and only see reviews for their selected category.
Okay so let’s say I have 10 total reviews and only 5 of them are assigned to ‘Category A’. If the visitor filters the posts by ‘Category A’, they will still see pagination links as if there are 10 posts instead of the 5 that belong to ‘Category A’. I hope that makes sense. No errors in console or debug log.
Line 5 starts a little snippet just to make things easier later in th file. Setting a var to the value of a category ID if the category parameter is found in the URL. If not, we set the category ID to null.
Line 20 start the form that contains the wp_dropdown_categories()
function which outputs a list of my categories. We set the selected param to the same value as the category ID from line 5. The form action attribute is set to the main archive url for the reviews so that if a category is selected and the user is on any page other than the first, they’re pointed back to the root review archive url.
Line 47 just forces the form to submit when an option from the dropdown is selected.
Line 57 is just telling WordPress which page we’re currently on, then the rest of the query.
By adding the 'total'=> $query_reviews->max_num_pages
parameter to my pagination function, this gets outputs the correct number of pagination links since it uses the value of max_num_pages of my query rather than the main query. Crisis averted.