Conditions: how to render elements only when post query meets specific criteria?

Currently, I am stuck in the following situation:
I have a query loop with a meta query that is pulling out content when the current date is smaller than a ‘post expiry date’.

Above (Outside of) that query loop I have elements (Buttons/ banners) that should only render when actual query loop content is meeting above meta criteria - so when stuff is actually displayed inside of the query loop.

A dynamic element condition on elements outside of the loop such as: custom post type “post expiry date” >= “current date” does not seem to do the trick.

My question is: How would I be able to render elements only when a query meets certain criteria?

1 Like

Good morning

Is there a reason why you can’t include the buttons / banners within the existing content query?

Anway, if that is not possible, the way I would do it is make a copy of your existing query (the content query) above it so that you can check if the query (this one and the content query) meet certain criteria and then based on the result, show your buttons / banners or hide them in the query above.

The problem is that outside of the query, you don’t have access to its data and therefore you can’t use a condition.

Cheers

Patric

Thanks Patric, that is what I suspected.

The reason why the element is not inside the loop is that its a table of content element - that ‘summarizes’ the h2 tags of the loop.

Obviously, I would like this ToC element to disappear when no content is rendered inside the loop (expiry date).

So, I’ll have to let a query run for the ToC element and then pick up on that query with the conditions? Can you give me some pointers on how I can achieve this? I have a query that echos out either the number of posts that fulfil ‘meta’ criteria or a statement that none is available… How could I use this in combination with conditions?

Hi

there are many youtube videos about Bricks conditions.

I think a very good one is this one:

I guess this video will point you into the right direction. With conditions, I think you always have to test them yourself on your site.

A condition that I use many times inside my loops which only shows the text element if {post_terms_post_tag} (=taxonomy terms assigned to a post) in the found post are not empty, is this one:

Foundposts

Cheers

Patric

Thanks, Patric - I will give it a try!

So Just to feedback to the community - I have managed to implement a solution. After the watching the (very good) video above - I stumbled upon this:

Conditionally Outputting Based on Query Count in Bricks – BricksLabs

which triggered me to write my own code:
The idea being that you have a query that counts the number of posts that meet a certain criteria, in my case that their expiry date (‘job-posting-period’) is higher than the current date. This number is the rendered out (via echo) and can be compared in the conditions.

<?php
function count_custom_posts() {
    $today = time();
    $args = array(
        'post_type' => 'job-postings',
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'job-posting-period',
                'value' => $today,
                'compare' => '>',
                'type' => 'NUMERIC'
            )
        )
    );
    $query = new WP_Query( $args );
    $count = $query->found_posts;
    wp_reset_postdata();
    return $count;
}
?>

With the help of WPCode box I have this php snippet implemented. Now I can use it in conditions like so:

image

Thanks for the help and the prompts!

3 Likes