Support

Account

Home Forums ACF PRO Filter customs posts with relationships

Solving

Filter customs posts with relationships

  • 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:
    https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    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:
    myurl.fr/clients/?clients_status=active

    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:
    mywebsite.com/clients/?clients_status=Active

    It works!

    But when I try to filter by ‘Types’ with the same type of url, it does not work.

    Example:
    mywebsite.com/clients/?clients_clienttype=b2b2c

    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.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Filter customs posts with relationships’ is closed to new replies.