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
Have you manage to get this working? Can you share you code if possible
Much appreciated
Cheers
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.