Home › Forums › General Issues › Hide values that have been selected in other post
Hi i hope anyone help me solve my problem.
I have a relationship field called ‘Related Tracks’
My problem is i want to hide/disable a value once it’s already been selected or a post has been relate already.
Ex. In my Related Tracks(Relationship feld) i selected ‘New Post'(Post) So once i added a new Related Tracks the post ‘New Post’ should be hidden or disabled so i can’t select it again.
thank you in advance
Hi there!
If I understand correctly, you want to filter all tracks that have a certain condition already defined on the select.
I did something similar using: acf/fields/post_object/query (https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/).
You need to a filter that will call the function that will narrow down the choices using a query.
In my case (this is OOP code but you should get the gist), I added the filter:
$action_filter_register->add_filter( 'acf/fields/post_object/query/name=client_match_object_field', Match::class, 'filter_participant_options', 10, 3 );
And then defined the function that did the trick:
public function filter_participant_options( $args, $field, $post_id ) {
$allowed_states = EntityStates::get_allowed_states_for_matching();
$args['numberposts'] = -1;
$args['meta_query'] = array(
array(
'key' => '_state',
'value' => implode( ',', array_keys( $allowed_states ) ),
'compare' => 'IN',
),
);
return $args;
}
Hope this helps!
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
https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
This could very likely time out the field and stop it from showing results.
The only way that I can thing to do this (well, the only way without querying the db directly) is to build a filter as suggested by @csaborio and in the filter get all other posts of the same time and look at what’s already been selected.
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
As I said, depending on the number of “Album” posts that exist this will eventually time out the AJAX request for the relationship field and would not be very scale-able.
You could potentially query the post meta table directly to get the acf field value for published posts.
Another possibility would be to create and acf/save_post. In this filter, when saving an “Album” post you could set an option (get_option()/update_option()) and store a list of selected tracks.
Yet another option would be to make the relationship bidirectional with a relationship on the Album side and a post object field on the track site. Then you could do something like this.
add_filter('acf/fields/relationship/query/name=related_tracks', 'remove_already_selected', 10, 3);
function remove_already_selected($args, $field, $post_id) {
$args['meta_query'] = array(
'relation' => 'OR'
array(
'key' => 'bidirectional_post_object_field_name',
'value' => ''
),
array(
'key' => 'bidirectional_post_object_field_name',
'compare' => 'NOT EXISTS'
)
);
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 @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
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.