Query Loop + Meta Query Issue

I have a CPT called Partners with associated ACF fields, one of which is ad_image (a basic ACF Image field set to return an array). A few partners have paid for ads and have an image in the ad_image field. All other Partners have a blank ad_image field. On the homepage, I have a slider where the slide is a loop querying Partners with a meta query of ad_image Exists. Initially, when I first build the website, this worked perfectly. After a couple of months, of the client adding additional partners, the slider is all messed up. It seems to be returning slides for Partners that do not have an ad_image set so, most of the time, the slider is blank. Occasionally things line up and there will be 1 or more images present. Am I doing the query loop wrong? I can’t figure out any other way to do it. Only want to display slides where the Partner’s ad_image field has an image…

Can you post a screenshot of the query, the meta, and maybe the ACF field - anything you think will help. It’s very difficult to offer suggestions without more info.

What I can offer is ‘what happened’ - what changed… if it worked, what site changes, SW updates etc. have occurred. Back track and the answer may be there. Can you migrate it to a staging site and backdate Bricks to 1.7 - just to see? Have you changed an ACF filed name and not updated something that depends on it. All very basic… but I know I forget the basics often enough!

1 Like

Here are screenshots of the ACF field, the ACF field in the Partner CPT, the Query Loop, and the Meta Query.

I handed the website off with about 100 partners that were loaded into the database via WP All Import. At that time, everything was working as expected. Since handing the website off, the client has been manually adding Partners via the CPT interface. That is the only difference I can think of is the way the data is getting into the database. I do a Partner export and see what the data for ad_image is looking like. It’s just really weird.

Screenshot 2023-06-29 at 14-52-24 Edit Partner “29 Branded Beef” ‹ HDFFA Food & Farm Directory — WordPress

My initial thought is that ACF is returning an image array. Sounds like it’s only ever one image…Maybe returning an image ID would be better? Depending on how you display it. Bricks accepts ID as dynamic data in an image block. Then use a numeric >0 compare on the meta.

Doesn’t really deal with why it once worked… but I know Bricks updates have added better data handlers, maybe one of these now treats an array as null if unspecified - which might break your meta condition.

1 Like

Hey @rogueDesign,

the problem with your meta query is that for every new member the ad_image key exists in the database. So your EXISTS comparison is always true.

Change your meta query to the following:

I’m using the __return_empty_string helper function from WordPress for the comparison as just leaving the field empty does not work.

For copying purposes: {echo:__return_empty_string}

Let me know if that helps.

Best,

André

2 Likes

Thanks, this is exactly what I needed! It’s working perfectly now.

This really helped me too. I’m very surprised this is not a function built-in. I mean it’s so un-intuitive, how would anyone know this?

I totally agree! This one got me today. I was trying to use EXISTS on a text field which did not work - probably because an empty string was being stored but Not Equal To and an empty value did not work either. It was only when I had not equal to {echo:__return_empty_string} that it worked.

Have things changed since this issue / post or is this still the way to make it work?