How to show/hide a section when a post doesn't have posts related (with Metabox relationships)

Hello! :wave:

In a Service page (CPT) I am showing related FAQs (another CPT) set by a Metabox relationship.

So far, so good!

Now, I want to hide the complete section if the Service doesn’t have any FAQ related.

I have followed some directions found in this forum and, although everything seems logical, I can’t get it to work as expected.

In WPCodeBox2 I have added this code snippet:

Then, in Bricks in every service page I am adding this, under Conditions:

However, it doesn’t work! :face_with_raised_eyebrow: The section disappears no matter if the post (Service) has FAQs related or not. If I set it to ==, then in every service page the FAQs section re-appear, has or has not a FAQ related.

Finally, for more context, this is the Relationship set in Metabox:

Everything seems logic and fine, but I can’t make it work, I don’t know why :exploding_head:

Any ideas?

Thank you very much!!!

Hey @knut_studio,

I did not try it myself yet but have_posts() in your function returns true or false. So can you try to use == (instead of !=) and 1 instead of an empty string in the Bricks condition?

Best,

André

Hey André! :grinning:

Thanks for trying to help me out!!

I have followed your instructions and changed the condition to check if {echo:post2post(‘faqs-services’) == 1.

However, if I set it to == 1, the section disappears from every page (either the ones that have a FAQ related and the ones that doesn’t have it).

If I change it to == 0, then it re-appears in every page again (also the same behavior on pages with and without FAQs related) :confused:

This is exactly what I’m trying to accomplish as well - hide a related section when there are no related posts. Were you able to get this working, or find an alternate solution?

Thanks!

I haven’t been able to get this working, yet!

If you or someone has any hint or idea, I would be much appreciated!

Hey @knut_studio,

I created a similar setup using Meta Box and for me it works as expected:

  1. Created a post type called “Service”
  2. Created a post type called “FAQ”
  3. Created a relationship called “Service to FAQ” (service-to-faq)
  4. Created a helper function to check if a Service has FAQ:
function has_faq() {
    if ( ! is_singular( 'service' ) ) {
        return false;
    }

    $faq = MB_Relationships_API::get_connected( [
        'id' => 'service-to-faq',
        'from' => get_the_ID(),
    ] );

    return ! empty( $faq );
}
  1. Used this function as a condition in the Service (Single) template:

Result: The section was correctly shown / hidden depending on the connected FAQ.

Best,

André

Hey André!

Thank you again for trying to help me on this!

However, it is still not working on my end. I don’t get why!

I am using your exact same function:

BUT, changing:

  1. is_singular(‘service’) for is_singular(‘esdeveniments’), that’s the id of my CPT.
  2. ‘service-to-faq’ for ‘faqs-services’, that’s the id of my Relationship.

Everything else stay the same.

But it is not working.

When the condition is == 1, then in every service page the FAQs section is hidden (not matter if it has faqs related or not). If then I change it to == 0, then the FAQs section shows in every page :confused:

Hey André!

I have finally managed to make it work, using a different approach that somebody’s point me to! :tada:

When setting the condition, you need to choose:

  • Dynamic Data
  • Then look for the Metabox relationship under your CPT
  • Set it to != and leave the following field as blank

The problem is that in Bricks you can’t see clearly the Relationship field when you click on Select Dynamic Data, and I am not sure if this is a bug.

What I see is like a strange gap. And when I click to it, then the function appears properly:

After selecting this weird field, it works perfectly:

Thank you for your help! :slight_smile:

2 Likes

Oh wow, this is amazing. After setting it up with one of the ‘hidden’ options, my element is now showing/hiding as expected. Thanks very much for sharing this…hate to admit how many hours spent trying to solve this trying multiple query scripts. :expressionless:

In my case, I actually have 3 relationships established, so I had 3 of those tiny compressed options hiding in the wings and needed to select each one until it showed the correct relationship. After that, voila!

Anyway, thanks again! I’m going to dig into the Bricks bug reports and idea board to see if this situation has been mentioned and is on their radar.

-Gary

this totally worked! it was exactly what I was expecting to do, except I wasn’t seeing that glitched out field in the drop-down. Thank you!

what’s the proper way to report this as a bug?

1 Like