NO BUG: Issues with query_results_count outside the loop

I am using Bricks 1.9.6.1 and the latest Metabox version. I’ve setup a condition that uses {query_results_count:queryid} that is several metabox groups deep but I can’t seem to get it working.

I am using Metabox and have a CPT setup with custom fields. Within the custom fields I have several groups within groups which requires me to have multiple queries setup in the structure to get the data I want.

image

The data displays correctly but the condition is not working on the list that is checking the query on the list item.

image

Basically I want the whole list to disappear (ie the ul tags) if there are no list items.

I am wondering if query_results_count is having issues because the metabox field group is 3 levels deep?

I’ve tripled checked the query id and it is correct (the list item id).

Hi Tom,
Thanks so much for your report!

Does it work if you use this condition instead?

If not, could you send temporary login credentials and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase?

Best regards,
timmse

Thanks for the reply.

I tried your condition idea and it didn’t work either.

I did try the 2 other queries on the page and they return a count (with one of them being the parent group of the list item group) and they worked. List Item shows a 0 though (instead of 2)

image

Also within the List Item query i can use just {query_results_count} without the id and it returns 2 which is the correct result but obviously doesn’t help me here since I want to disable the list if no list items.

I will send the login details to the website via support.

Thanks

I saw someone had logged in. Any update on the issue?

Hi @Tom ,

As per my email reply, {query_results_count} in Bricks only support current loop or 1 level deeper loop check.

Based on your scenario, it’s a 2 level deeper check, hence you can’t get the correct count.

You can use this snippet + {echo} in condition

function mb_group_subfield_count( $group, $subfield ) {
    if( empty($group) || empty($subfield) ) {
        return 0;
    }

    $group = rwmb_meta( $group );
    if ( empty($group) ) {
        return 0;
    }
    
    $subfield_count = isset( $group[$subfield] )? count( $group[$subfield] ) : 0;

    return $subfield_count;
}

// Check list items count inside safety_rules_section
{echo:mb_group_subfield_count('safety_rules_section','list')}

Regards,
Jenn

2 Likes

Thanks for providing the snippet, it works perfectly!

Hi!
I probably have the same problem.
What code do you recommend if I use ACF?

Hey guys,

I stumbled upon this thread when looking for a solution to having loops more than 1 level deep and still using conditions to hide their parent containers.

Could someone explain to me the loop depth in a bit more context? I don’t quite understand in this thread, or in the official docs: " {query_results_count:quer34} only works if the target query is 1 level deep. You wouldn’t be able to get the correct count if the target query located in a >= 2 level deep nested query."

In case it helps another searcher - I wound up using a CSS approach which is a little hackier but works well if you don’t mind the container continuing to appear in the DOM, albeit hidden.

  1. Assign the child block (with your levels-deep query loop) a class like monitored-block.
  2. Assign the parent container/whatever a class like conditional-container.
  3. Set up a Bricks condition on the child block using whatever method, I used query_results_count so the block is hidden if no loop contents.
  4. Leverage that block disappearing to also hide the parent container with some CSS on the container’s Style tab:
.conditional-container:not(:has(.monitored-block)) {
  display: none;
}

And the CSS logic will add display: none; to your parent container.

As I said not as elegant as a custom function but for simple cases (like mine) it is doing a job that I spent too long trying to work out!

Cheers,
Tom

1 Like

It’s explained pretty well by Itchycode and you look to have a handle on understanding the issue with your css work around.

The code snippet is for Metabox. For ACF you would have to change the snippet to work with it.

Look at my initial example and screen shots…

instead of {query_results_count:xucbdh}

I needed to add (along with the code snippet obviously):

{echo:mb_group_subfield_count(‘safety_rules_section’,‘list’)}

The code snippet initiates a query to get the results count for the Metabox group “safety_rules_section” and subfield “list” which in my Metabox setup are a group of custom fields.

1 Like