12th Dec '23
/
0 comments

How to Modify Products Element Query in Bricks to Output Child Products

This Pro tutorial provides the steps to limit WooCommerce products output by Bricks' Products element on single product pages to only the children of the current product.

Step 1

Let's make the product post type hierarchical.

Add the following in child theme's functions.php or a code snippets plugin:

<?php add_filter( 'woocommerce_register_post_type_product', function ( $args ) { $args['hierarchical'] = true; $args['supports'][] = 'page-attributes'; return $args; } );

Edit your products and set up parent-child arrangements as needed.

Step 2

Edit your single WooCommerce product template with Bricks.

Add a Section and inside its Container, an optional heading and Products element.

Step 3

Let's

  1. define a custom function that returns an array of current post's (of any post type) child posts and the count of the same
  2. hook a function to bricks/query/result filter to limit the products to only the child products on single WooCommerce product pages

Add the following in child theme's functions.php or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 624 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
How to Redirect WordPress Search to WooCommerce Shop Filter by Product Title or SKU

How to Redirect WordPress Search to WooCommerce Shop Filter by Product Title or SKU

Learn how to redirect users to the Shop page with filtered results if the searched term matches a WC product title or SKU. And, if…
Categories:
Pro
x Number of Random Bricks Query Loop Items(s) to be Shown Every x Hours

x Number of Random Bricks Query Loop Items(s) to be Shown Every x Hours

Showing how to display one random row of a Meta Box group for an hour and then output another random row.
Pro
Outputting Only the First ACF Repeater Row in Bricks

Outputting Only the First ACF Repeater Row in Bricks

Updated on 12 Dec 2023 In the Bricks Facebook group a user asks: How can I display only the first entry from an ACF repeater?…
Pro
[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

[WooCommerce] Sticky on Scroll Add to Cart section in Bricks

Setting up a sticky section that fades in when scrolling down and fades away when scrolled to the top.
Categories:
Pro
How to Inject a Different Type of Post in Bricks Query Loop

How to Inject a Different Type of Post in Bricks Query Loop

Updated on 26 Aug 2024 A user asks: Hello, I'd like to inject a CPT post card inside of a Bricks query loop of a different CPT…
Pro
On Sale Products Bricks Query Loop

On Sale Products Bricks Query Loop

Looking to show only the WooCommerce products that are on sale in Bricks? Set up a query loop, enable the PHP query editor and paste:
Categories: