Display a specific category of products in shop page

Question

I want to display a specific category of products on the top page of the shop page.
By default, all products are displayed.
Appearance> Customize> woocommerce
You can edit from here, but you couldn’t do what you wanted.
What you want to do is:
1, Display only related products of a specific category on the top page of the shop page
2, From the category and tag link of each product page, it is displayed on the shop page properly. Products other than the specific category-related product selected in “1,” are also displayed.
3, Searching on the search console finds all products. At this time, products other than the specific category-related product selected in “1,” are also reflected in the search results.

To meet the above three conditions, I tried the code with reference to another question.
In the attached code, “1” and “2” were cleared, but “3” could not be cleared.
In other words, products that are not related to a specific category will not be found in the search.
How can I make changes to search for products other than category-related products?

add_action('pre_get_posts','shop_filter_cat');

function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( '#','#'), //
)
)
); 
}
}

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/display-a-specific-category-of-products-in-shop-page/">

Display a specific category of products in shop page</a>
0
, LMDM 2 months 0 Answers 69 views 0

Leave an answer