最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Display products from specific category in shop page

matteradmin5PV0评论

I know many people asked for this question but I didn't find a proper way to do it. How to add a simple meta_query (product_cat) before the execution of the shop page's query.

Maybe by using a filter ?

Regards,

Adrien

I know many people asked for this question but I didn't find a proper way to do it. How to add a simple meta_query (product_cat) before the execution of the shop page's query.

Maybe by using a filter ?

Regards,

Adrien

Share Improve this question asked Apr 28, 2013 at 10:47 miko7358miko7358 4062 gold badges7 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 19

The shop page is actually an archive page for posts of type 'product'. Its template is in woocommerce/archive-product.php.

You need to use the pre_get_posts action to preprocess the query before the loop, conditional_tags to recognize that you are in the product archive page, and a taxonomy query to filter the product categories, which belong to the taxonomy 'product_cat'.

For example, the following (placed in your theme's functions.php or in a plugin) will display only products with product category 'type-1':

 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' => 'type-1'
                                 )
                     )
       );   
    }
 }

You can also exclude categories by using 'operator' => NOT IN, and 'terms' can be an array of product category slugs.

A good introduction to query customization is http://www.billerickson/customize-the-wordpress-query/

This worked for me:

[product_category category="YOUR CATEGORY" per_page="8" columns="3" orderby="date" order="desc"]

If you want to show products from a specific category on your shop page you can insert the below code in your themes function.php file.

// Execute before the loop starte
add_action( 'woocommerce_before_shop_loop', 'techlyse_before_action', 15 );
function techlyse_before_action() {
    //To ensure Shop Page
    if ( is_shop() ) {
        $query_args['tax_query'] =  array(
            array( 
                'taxonomy' => 'product_cat',   
                'field' => 'id',  //If you want the category slug you pass slug instead of id and 1330 instead of category slug. 
                'terms' => 1330 
            )
        );  
        //print_r($query_args);
        query_posts( $query_args );
    }
} 

// Execute after the loop ends
add_action( 'woocommerce_after_shop_loop', 'techlyse_after_action', 15 );
function techlyse_after_action() {
    //To ensure Shop Page
    if ( is_shop() ) {
        //Reset the Query after Loop
        wp_reset_query();
    }
}
Post a comment

comment list (0)

  1. No comments so far