Hello, I’m currently working on filtering custom posts.
The idea is to create a filter space to sort these customs posts. I am based on the following tutorial:
It works well rather. For example, I can sort my customs fields, which are customers of a company, by their status ‘Active’ or ‘Inactive’.
Concretely, this gives this url:
This works because clients_status is a field corresponding to the custom field ‘Clients’.
But here is my concern:
I have another custom field which is a relation with the customer custom field. It is a multiselect for which each customer has one or more data.
How can I get the list of options for this relationship in my template archive-clients.php and above, what url format to use at the time of sorting?
Thank you in advance for your return !
Not sure if you’re using a relationship field or a user field by your description, but either of these are similar. They hold an array of IDs to either posts or users. There is no way to filter these easily if you want to select more than one value.
You should look a the Multiple custom field values (array based values) section of https://www.advancedcustomfields.com/resources/query-posts-custom-fields/.
As these are ID values what you see in the example here
array( 'key' => 'location', 'value' => 'Melbourne', 'compare' => 'LIKE' ),
Should be replaced by something like
array( 'key' => 'location', 'value' => '"'.$ID.'"', 'compare' => 'LIKE' ),
as in the single-location.php section of https://www.advancedcustomfields.com/resources/querying-relationship-fields/
Hello John and thank you for your return, it helped me!
I have one last question regarding the sorting of my contents.
So I have a Field group called ‘Clients’.
In this field group, I find:
‘Status’ which are checkboxes
‘Types’ which is a select with different choices.
In my template archive-clients.php, I set up a sorting system that lists all possible ‘Status’ and ‘Types’.
When I click on one of the ‘Status’ checkboxes, it filters my clients with the selected status, with this url:
But when I try to filter by ‘Types’ with the same type of url, it does not work.
The URL must be different for data with checkboxes and a select?
Thank you in advance !
The query and the way you use the value in the query will need to be different using the information I posted about queries based on fields that hold arrays.
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!
© 2019 Advanced Custom Fields. Subscribe