11th Dec '23
/
1 comment

Related FAQs on Product pages Based on Product Category Taxonomy in Bricks

Updated on 20 Dec 2023

This Pro tutorial provides the steps to modify a Bricks query loop's posts (of a CPT) output to those related to the current post's (another CPT) terms using a Taxonomy type field.

Scenario

CPT: faqcustom field: product_category of type Taxonomy created using ACFreturn type: ID

FAQ posts have 1 or more product categories associated with them.

Objective

On single product pages, show all the FAQs associated with the current WooCommerce product's product categories (which again, can be single or multiple).

Solution Approach

  • Create a PHP function that loops through all posts of faq post type, and for each faq, loop through the values of its product_category ACF field of type taxonomy and add the faq to the array of faqs for that product category. This function returns an associative array of faqs with product category ID as the key and corresponding faqs' array as the value.
  • We create another function that gets the current product's product categories, loops over them, checks if the product category ID is in the earlier function's output, and if so, merge its value (array of FAQs) with a new FAQ array in each iteration.
  • Another function that returns the count of the earlier function so the entire Section of "Related FAQs" can be conditionally output only if there's at least 1 related FAQ item.
  • Finally, we set unique faq post IDs of this new FAQ array as the value of post__in query parameter.

Sample Data

Numbers in brackets indicate the post/term ID.

FAQ 1: product_category = Automotive (66)

FAQ 2: product_category = Automotive (66), Baby (71)

FAQ 3: product_category = Watch (64)

View: Single product pages

Any product that has Automotive as its product category should show these FAQs: FAQ 1 (393) and FAQ 2 (394)

Any product that has Automotive and Baby as its product categories should show these FAQs: FAQ 1 (393) and FAQ 2 (394)

Any product that has Baby as its product category should show this FAQ: FAQ 2 (394)

Any product that has Watch as one of its product categories should show this FAQ: FAQ 3 (395)

Step 1

Create a faq CPT using ACF.

Register an associated ACF field group tilted say, "FAQ Fields":

Step 2

Create a few FAQ entries and for each, select one or more corresponding product categories.

Step 3

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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
ACF Term Meta on Single Posts in Bricks

ACF Term Meta on Single Posts in Bricks

Consider this scenario: CPT: Project Custom taxonomy: Project Type Each project will be assigned only one project type. Project Type taxonomy's custom field: Project Type…
Categories:
Create A Customizable AJAX Add To Cart Button In Bricks Builder

Create A Customizable AJAX Add To Cart Button In Bricks Builder

In Bricks, you can simply create an Add To Cart button from a dynamic data / function {woo_add_to_cart}. This button supports AJAX as well if…
Pro
Merging ACF Gallery Images with Featured Image in Bricks

Merging ACF Gallery Images with Featured Image in Bricks

Updated on 12 Mar 2025 In the Bricks Facebook group a user asks: Merging images from different fields into a single gallery Hi, I have…
Categories:
Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…
Pro
Random images from ACF Options Gallery in Bricks

Random images from ACF Options Gallery in Bricks

This Pro tutorial provides the steps to output images from an ACF Gallery-type custom field in a Bricks query loop randomly with each image appearing…
Categories:
Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories: