'How to limit the number of wordpress search results from each category on each result pagel
I am using the code below to show search results for different post types separately/segmented form.
if (have_posts()) {
// In the below line of code you can set the loop order in array (1, 2, 3, 4)
// You can also turn a post type off by removing it from the below line.
$types = array('post', 'page', 'sfwd-courses', 'product');
foreach ($types as $type) {
// Here you can customize both the titles, section heading title & side title
if ($type == 'post') {
$head_title = "Results in Posts";
$side_title = "post";
}
if ($type == 'page') {
$head_title = "Results in Pages";
$side_title = "page";
}
if ($type == 'sfwd-courses') {
$head_title = "Results in Courses";
$side_title = "course";
}
if ($type == 'product') {
$head_title = "Results in Products";
$side_title = "product";
}
// This div below shows up the head title for each section
echo '<div class="search-section"><h4>' . $head_title . '</h4></div>';
while (have_posts()) {
the_post();
if ($type == get_post_type()) {
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article">
<header class="article-header">
<div class="article-header-content">
<h3 class="title head-2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<!-- This div below shows up the side title for each section -->
<div class="posttypetext"><i> <?php echo $side_title; ?> </i></div>
</div>
<!-- <?php get_template_part('parts/content', 'byline'); ?> -->
</header> <!-- end article header -->
</article> <!-- end article -->
<?php }
}
rewind_posts();
}
} else {
get_template_part('template-parts/content', 'missing');
}
This shows me a list of search results showing all the posts first and then the results from pages, then courses and finally products.
The pagination limit right now is 20 per page.
I want that on each page I should get 5 search results from post, 5 from page, 5 courses and 5 products.
this is needed because if there are 30-40 posts results, it will cover up the first 2 pages of search results and users will not see any courses or products that will be lying on the 3, 4 page.
I hope it makes sense, is there a way to accomplish that?
Thanks JS
Solution 1:[1]
There are multiple ways to set this up. The simplest way is to take advantage of the current_post property of your query which would give you the index number of the current post.
So in your code you could replace this line:
if( $type == get_post_type()){
// your template section
}
With this line:
global $wp_query;
if( $type == get_post_type() && $wp_query->current_post <= 4){
// your template section
}
It's $wp_query->current_post <= 4 because current_post is zero based and you only need to output the first 5 posts!
There are others ways to achieve the same thing using wp_query, but you haven't provided any details about how you've created your query. You just provided us with your loop template, we don't know how it's been queried and where this loop is located. So I think using current_post property would be simplest way here!
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 | Ruvee |
