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

Related Tutorials..

Pro
ACF User Field Bricks PHP Query

ACF User Field Bricks PHP Query

In the Bricks Facebook group a user asks: my end goal was to create a set of cards that would display the Users that are…
Categories:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Update on 8 Sep 2024: You may want to follow the newer version that considers accessibility. Looking to make a Horizontal Image Accordion dynamic? This…
Pro
Recently Viewed Posts Bricks Query Loop

Recently Viewed Posts Bricks Query Loop

How to output the most recent X number of posts (any post type) viewed by the current visitor.
Categories:
Pro
Current Post’s Terms in Bricks

Current Post’s Terms in Bricks

Updated on 15 Jan 2024 This Pro tutorial provides the steps to set up a grid of terms (like categories) that the current post is…
Pro
Query Loop Index in Bricks

Query Loop Index in Bricks

This Pro tutorial provides the code for getting the index of each post in Bricks' query loops. This can be used for either outputting the…
Categories:
Pro
Nav Menus Custom Query Types in Bricks

Nav Menus Custom Query Types in Bricks

Updated on 21 Aug 2024 This Pro tutorial shows how custom query types for each navigation menu can be generated in Bricks. This enables us…
Pro
Search Results Grouped by Post Types in Bricks

Search Results Grouped by Post Types in Bricks

Updated on 08 Oct 2024 This Pro tutorial provides the steps to arrange the search results by specified post types in a Bricks site. We…