1

Assuming I have these custom post types:

  1. Product
  2. Video
  3. Article

And for Product (WooCommerce Product), we have many Categories, one of which is called "Parts".

I have this situation where I need to sort my search query using the following rule:

  1. Product (that is not Parts), then
  2. Product (Parts), then
  3. Video, then
  4. Article

That means if I search a keyword, say "engine", Products should come out first (with main products coming out before parts). Then Video and Article should come later.

Is it possible to capture such a use case?

I've solved the issue with "Sort By Post Type", referring to this post: Sort search results by post type

What puzzles me is, how can we sort by Post Type and then by Taxonomy, but only in the case if the post_type is Product.

Any idea?

1 Answer 1

1

yes, offcourse it is possible, I had made two websites for selling car pieces myself and that is very possible. anything is possible.

you can create your own search results with the file search.php

here are two ways of getting products info:

$args = array(
       'post_type' => 'product',
       'posts_per_page' => 10,
       'meta_key' => '_last_viewed',
       'orderby' => 'meta_value',
       'order' => 'DESC'
      );
            
query_posts( $args );

if( have_posts() ) :
  while( have_posts() ) : 
    the_post();
    echo the_title();
  endwhile;
endif;
$args = array(
        'post_type' => 'product',
        'posts_per_page' => 10,
        'orderby' => 'date',
        'order' => 'desc',
       );
query_posts( $args );

if( have_posts() ) :
  while( have_posts() ) : 
    the_post();
    echo the_title();
  endwhile;
endif;

I hope this can give an idea on how to get your goal

to have exactly what you need only having more specifics.

in this two examples, can be searched the first and display it, than do another search with the requiremens needed and display the second results and so foward. if we dont want something to be display we make ways to filter the info and not displaying it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.