Aleš Sýkora / November 28, 2023 / 0 comments

Bricks builder WooCommerce Queries

Post summary: WooCommerce TOP selling products query in Bricks You need to use Query editor for this and add this code: WooCommerce Featured products query in Bricks To display featured products only, use this in your Query Loop: Retrieve WooCommerce “NEW” products added not more than 30 days ago Bricks builder currently lacks the Date Query, however,…

WooCommerce TOP selling products query in Bricks

You need to use Query editor for this and add this code:

 return [
    'post_type'      => 'product', 
    'posts_per_page' => -1, 
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'total_sales',
    'order'          => 'DESC', 
];

WooCommerce Featured products query in Bricks

To display featured products only, use this in your Query Loop:

  • Query type = post
  • Post type = products
  • Posts per page = -1 (change to your needs)
  • Ignore sticky posts = true
  • Tax query =
    • Taxonomy = Product Visibility
    • Field = name
    • Terms = featured

Retrieve WooCommerce “NEW” products added not more than 30 days ago

Bricks builder currently lacks the Date Query, however, you can construct your query params with PHP. Be aware about using this query if you do not add new products periodically. If you do not, then please just use order by date.

return [
'post_type' => 'product',
    'posts_per_page' => 25,
    'orderby' => 'date',
    'order' => 'DESC',
    'date_query' => array(
        array(
            'after' => '30 days ago',
        ),
    ),
];

If you do not add products every 30 days, you may just use order by date:

WooCommerce Products on sale query

How this works in WordPress

$args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10, // Number of products to display
    'meta_query'     => array(
        'relation' => 'OR',
        array(
            'key'     => '_sale_price',
            'value'   => '0',
            'compare' => '>',
            'type'    => 'NUMERIC',
        ),
        array(
            'key'     => '_min_variation_sale_price',
            'value'   => '0',
            'compare' => '>',
            'type'    => 'NUMERIC',
        ),
    ),
);

$on_sale_query = new WP_Query($args);

if ($on_sale_query->have_posts()) {
    while ($on_sale_query->have_posts()) {
        $on_sale_query->the_post();
        // Output the product information here
        the_title();
        the_content();
        // Add more product information as needed
    }
}

wp_reset_postdata();

How you set this up in Bricks?

In this code:

  • 'post_type' is set to 'product' to specify that we want to retrieve WooCommerce products.
  • 'posts_per_page' determines the number of products you want to display.
  • 'meta_query' is used to filter products based on their sale price. We use two conditions: one for regular products and one for products with variations.
  • We check if the _sale_price or _min_variation_sale_price meta keys have a value greater than 0 to identify products on sale.
  • The 'type' is set to 'NUMERIC' to ensure that the values are treated as numbers for comparison.

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts