I’m trying to make a custom archive with filters (like this tutorial) on two fields. Although both fields are type=Select, just like the tutorial, one of them allows multiple values, and that seems to be causing problems. A URL like http://thinkword.com/events/?event_category=&day_of_week=Sunday (day_of_week is the field that can have multiple values for a given event) should find records – at the moment there are two events with Sunday selected. But apparently they are not found. The other field works fine with identical code – you can try http://thinkword.com/events/?event_category=worship&day_of_week= and it will work.
The second problem is that when the filter finds no matching records, get_field_object() doesn’t return the array of field information needed to to build the sets of checkboxes. I don’t know why that happens, but it happens in the tutorial, too – look at 11:05 on the video and you’ll see the checkboxes disappear (his doesn’t have an error like mine does, and I could of course prevent the error, but I want the checkboxes!).
Here is my entire in-progress template (I’m using StartUpPro theme, if that matters):
Ignore the first question – I gave up completely on allowing multiple selections, as it ran into too many problems and the case where we might need more than one is purely theoretical, so I decided to keep it simple for now.
But the second issue needs solving if I’m going to have these filters at all. As I said, the problem can easily be seen on the tutorial video at timecode 11:05, so it’s not something unique to my code. If anyone else has implemented such a filter, how did you deal with get_field_object() failing when the result set is empty? This looks like a bug in get_field_object() to me.
Okay, I finally figured this out on my own. I studied the get_field_object() function more deeply, and on this documentation page, in the “field_key vs field_name” section, there is a hint. It is not said directly, but apparently even if posts have been created that use the Field Group, if a filter causes the current query of posts (using the pre_get_posts hook) to return none of them, field_name won’t work. Even Elliot used the field_name in his tutorial, but in the case of a filter like this, you MUST use the field_key, even though it makes the code harder to read.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.