19th Oct '24
/
3 comments

Related Posts by Taxonomy in Bricks

Related Posts by Taxonomy in Bricks

In the previous Posts Related by Current Post's Terms in Bricks tutorial, we showed how posts related to the current single post that have the same terms as the current post can be configured in a Bricks query loop.

That method is not reliable esp. in situations when the terms have commas and other special characters in them. This tutorial addresses this question a user asked in the comments section there:

I got this working on my page but for some reason it's not working for taxonomies that have commas in them, like "Cancer, Heart Attack, and Stroke" or "Dental, Vision, and Hearing" It displays fine for other Taxonomies without commas

This Pro tutorial provides the steps to use PHP query editor of a Bricks query loop to output x number of posts (of any post type) related to the current single post by a specified taxonomy.

After implementing the tutorial you could simply paste

return bl_get_related_posts_query();

in a query loop's PHP editor and sign code to output 5 posts that have the same category/categories as the current single post being viewed.

or for example,

return bl_get_related_posts_query( 'project', 'project-type', 7 );

to output 7 project CPT posts that have the same project-type custom taxonomy terms as the current single project being viewed.

Step 1

Add the following in child theme's functions.php (w/o the opening PHP tag) 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 626 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Non-empty events sorted by event date and other posts below in Bricks

Non-empty events sorted by event date and other posts below in Bricks

Updated on 15 Jan 2024 Consider a scenario where you are showing posts from multiple post types namely post, event and game in a single…
Categories:
Pro
Categories and SubCategories Query Loop Tabs in Bricks

Categories and SubCategories Query Loop Tabs in Bricks

Learn how to show top-level parent categories as tab titles and the corresponding subcategories as tab contents using Bricks nested queries.
Categories:
Pro
Post-specific ACF Repeater Galleries as Sliders in Bricks

Post-specific ACF Repeater Galleries as Sliders in Bricks

How to output ACF Repeater field rows with the images of the Gallery-type sub field as a slider.
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
Pro
Child Posts Section in Bricks

Child Posts Section in Bricks

This Pro tutorial provides the steps to limit the posts (can be of any post type) output by Bricks' query loop to children of the…
Categories: