• Resolved andrewtayloruk82

    (@andrewtayloruk82)


    Hi, I’m looking for help or guidance with setting up a filter.

    I have two custom post types: Data and Companies. The Data posts have an ACF relationship field that links them to one or more Companies. The ACF field is set to return the company post object.

    I want to create a dropdown filter on the Data posts archive page, where users can filter the Data posts by Companies. Only the Data posts associated with the selected company should be displayed.

    Does this sound like this would be possible?

    Thanks in advance,
    Andrew

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support fesupportteam

    (@fesupportteam)

    Hi @andrewtayloruk82

    We just tested it on our back with such ACF – https://prnt.sc/T8dAqunk8Geh

    Applied on our product – https://prnt.sc/61XaU8W96FxW

    And filter by it – https://prnt.sc/gAQK_-1GjsS7

    It displays the ID so it will require some custom coding in order to change the ID to the label, some code examples can be found here (label : value) approach – https://filtereverything.pro/resources/acf/#value&label

    Best Regards – Victor

    Thread Starter andrewtayloruk82

    (@andrewtayloruk82)

    Thanks Victor, I’m able to replicate the same as your tests now but the value label example in the link you provided doesn’t do anything as the relationship field returns an entire post object and I’m not sure the filter works any longer as the notice on the page says it only works on 1.8.5 and older. Even when I strip the code right back and try to replace term_name with a hardcoded placeholder it doesn’t do anything.

    Thanks in advance.

    Thread Starter andrewtayloruk82

    (@andrewtayloruk82)

    — Message removed —

    Plugin Support fesupportteam

    (@fesupportteam)

    Hi @andrewtayloruk82

    This is not the exact code but it should give an idea of how to convert the IDs into the labels. By default, this code was for a (label : value) functionality of the ACF. So instead of a value, it could show the labels, but in 1.8.6 this was integrated by default in the plugin functionality, but there is no functionality by default to convert the relationship fields data so far.

    Best Regards – Victor

    Thread Starter andrewtayloruk82

    (@andrewtayloruk82)

    Hi,

    I don’t want to ask for something that’s outside the scope of support i.e. I don’t want you to write any custom code but I can’t get any of your filters to change the output in the dropdown we’re using. Even if I strip all the ACF specific stuff out and just return a generic placeholder name the IDs are the only thing I can get to output in the dropdown. It’s as if the filters aren’t doing anything. Can you double check that you can get the wpc_filter_post_meta_term_name filter to override the IDs in the test dropdown you created?

    Plugin Support fesupportteam

    (@fesupportteam)

    Hi @andrewtayloruk82

    Yes, absolutely – https://prnt.sc/peCEr9nZaJrF but please note that after you apply your code, you need to go to the filter set and hit update. So it will refresh filter terms transitions. Perhaps that was the issue that you cannot change it.

    Even thought this is out of the scope of support policy, we already spend time on it to test it, here is the code example (you can improve it as it was written for testing purposes):

    add_filter('wpc_filter_post_meta_term_name', 'wpc_acf_labels_instead_of_values', 20, 2);
    function wpc_acf_labels_instead_of_values($term_name, $meta_key)
    {
    // Specify the meta keys for which you want to modify values ('relation_test' is out created custom field in the previous example)
    $meta_keys = array('relation_test', 'your_second_meta_key', 'third_meta_key');

    if (in_array($meta_key, $meta_keys)) {
    if (function_exists('acf_get_field')) {
    $field = acf_get_field($meta_key);

    if ($field) {
    // Check if the field is a relationship field by its type
    if ($field['type'] === 'relationship') {
    // Convert term_name (which contains IDs) into array if multiple
    $ids = explode(',', $term_name);

    // Initialize an empty array to store the post titles
    $post_titles = [];

    foreach ($ids as $id) {
    // Get the post object by ID
    $post = get_post($id);
    if ($post) {
    $post_titles[] = $post->post_title;
    }
    }

    // Join the titles into a string (if multiple)
    $term_name = implode(', ', $post_titles);
    }
    // For other field types, continue as before
    else {
    if (isset($field['choices']) && !empty($field['choices'])) {
    $new_choices = [];
    foreach ($field['choices'] as $key => $value) {
    $new_key = mb_strtolower($key);
    $new_choices[$new_key] = $value;
    }

    $term_name_test = mb_strtolower($term_name);
    if (isset($new_choices[$term_name_test])) {
    $term_name = $new_choices[$term_name_test];
    }
    }
    }
    }
    }
    }

    return $term_name;
    }

    Best Regards – Victor

    Thread Starter andrewtayloruk82

    (@andrewtayloruk82)

    Thank you so much for this. It worked perfectly.

    Have a great day.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.