4th Oct '23
/
5 comments

Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Rendering query-loop enabled posts depending on the value of each post's custom field value is tricky because by default, the custom field plugins' functions or builder-functionality for fetching the custom field pulls the value of meta for the post (Post/Page/CPT/template etc.) in which the query loop element is present, not the individual posts in the loop.

In a previous Conditional Rendering Based on Current Date and Time in Bricks when Using Meta Box tutorial we showed how a non query loop element can be output based on a Radio field in a Settings page.

In this Pro tutorial we use a similar type of field, a Select field with the same choices and see how the posts can be output depending on the selected choice for each post.

Field group created using Meta Box for post post type:

A specific post being edited:

The above post should be output only if the current time is between 9 am and 6 pm on a weekday.

Step 1

Set up your query loop the usual way.

Step 2

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

Related Tutorials..

Checking if the current post has been published within the last x days

Checking if the current post has been published within the last x days

Conditionally output an element if the current post has been published in the last x number of days.
Categories:
Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories:
Pro
Posts older than today in Bricks

Posts older than today in Bricks

How to display only posts that were published before today in a query loop.
Categories:
Pro
Condition for checking Bricks content

Condition for checking Bricks content

Only outputting Bricks content when a Page is built with Bricks, otherwise display WP content.
Categories:
How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories: