admin管理员组

文章数量:1277875

I've a small issue with the default search, I want to search cross different post_types and returning for example 5 items. In this moment if I search for the word: foo and this word is in 10 wc products, 3 posts and 8 pages the search will return the the 5 product but it will not return any posts or pages, what I would like to have is that the search return:

5 products
3 posts
5 pages

is it possible somehow?

I've a small issue with the default search, I want to search cross different post_types and returning for example 5 items. In this moment if I search for the word: foo and this word is in 10 wc products, 3 posts and 8 pages the search will return the the 5 product but it will not return any posts or pages, what I would like to have is that the search return:

5 products
3 posts
5 pages

is it possible somehow?

Share Improve this question edited Oct 22, 2021 at 12:48 Christine Cooper 8,8977 gold badges60 silver badges93 bronze badges asked Oct 16, 2021 at 16:38 francesco.venicafrancesco.venica 1011 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

My first guess will be to run multiple custom queries on your search.php template in order to change post_type and posts_per_page

It could look like that

<?php
$s = get_search_query();
$args_array = [
  {'s' =>$s,'post_type'=>'products','posts_per_page'=>5},
  {'s' =>$s,'post_type'=>'posts','posts_per_page'=>3},
  {'s' =>$s,'post_type'=>'pages','posts_per_page'=>5},
],
foreach( $args_array as $args ) {
  $the_query = new WP_Query( $args );
  while ( $the_query->have_posts() ) : $the_query->the_post();
    // YOUR CODE HERE
  endwhile;
wp_reset_postdata();

本文标签: search get minimum number of post for each post type