Support

Account

Forum Replies Created

  • Hi, @hube2

    Thank you so much on your response.

    i’ll test your suggestion and i’ll get back to you with the result.

    Thank you alot

    Cheers

  • Hi @hube2 i already fix the error i mentioned above and your code work like a charm. Thank you so much for your amazing help your such a big help to this community. I might need more help in the future because mostly e use ACF on our website.

    Once again thank you so much.

    Here the final code

    `add_filter(‘acf/fields/relationship/query/name=related_tracks_certificate’, ‘remove_already_selected’, 10, 3);
    function remove_already_selected($args, $field, $post_id) {
    $query = new WP_Query(array(
    ‘post_type’ => ‘Tracks’, // my assumption
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1
    ));
    $selected = array();
    if (count($query->posts)) {
    foreach ($query->posts as $post) {
    $tracks = get_field(‘related_tracks_certificate’, $post->ID, false);
    if (!empty($tracks)) {
    foreach ($tracks as $track) {
    $track = intval($track);
    if ($track && !in_array($track, $selected)) {
    $selected[] = $track;
    }
    } // end foreach track
    } // end if tracks
    } // end foreach post
    } // end if posts
    if (!empty($selected)) {
    $args[‘post__not_in’] = $selected;
    }
    return $args;
    } // end function

  • Hi @hube2 Thank you so much for your response, I appreciate it

    Upon using your code.

    add_filter(‘acf/fields/relationship/query/name=related_tracks’, ‘remove_already_selected’, 10, 3);
    function remove_already_selected($args, $field, $post_id) {
    $query = new WP_Query(array(
    ‘post_type’ => ‘album’, // my assumption
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1
    ));
    $selected = array();
    if (count($query->posts)) {
    foreach ($query->posts as $post) {
    $tracks = get_field(‘related_tracks’, $post->ID, false);
    if (!empty($tracks)) {
    foreach ($tracks as $track) {
    $track = intval($track);
    if ($track && !in_array($track, $selected) {
    $selected[] = $track;
    }
    } // end foreach track
    } // end if tracks
    } // end foreach post
    } // end if posts
    if (!empty($selected)) {
    $args[‘post__not_in’] = $selected;
    }
    return $args;
    } // end function

    There is error on this line ‘$selected[] = $track;’

    the error is syntax error, unexpected ‘;’

    Sorry im very newbie on coding.

  • Hi @csaborio Thank you so much for your response.

    Basically i want to filter the relationship field list of item to narrow down the choices to show only the item’s that is not selected or item not being relate to any post.

    I did ty your code but no success

    Thanks

  • @christian-denatorange-fr

    Have you manage to get this working? Can you share you code if possible

    Much appreciated

    Cheers

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