Home › Forums › ACF PRO › Relationship field – set all values as selected as default › Reply To: Relationship field – set all values as selected as default
I’m sure there are more eloquent ways to pull this off however give this a try. This will only work on relationship fields which are pulling in choices from custom post types.
function my_acf_load_value( $value, $post_id, $field ) {
// Loads current post
$post = get_post( $post_id );
// Check we are on the new screen
if ( $post->post_status == 'auto-draft' ) {
// Pull in custom post types from fields
$post_types = $field['post_type'];
// Fetches all post IDs choices
$post_ids = get_posts(
array(
'post_type' => $post_types,
'posts_per_page' => '-1',
'fields' => 'ids',
)
);
// Assign all choices as the default value
$value = $post_ids;
}
return $value;
}
add_filter( 'acf/load_value/type=relationship', 'my_acf_load_value', 10, 3 );
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!
ACF’s Repeater field is a versatile solution for repeating content in #WordPress.
— Advanced Custom Fields (@wp_acf) December 29, 2022
What’s your favorite use case for the Repeater field?
What’s the weirdest thing you’ve ever used it for?
Let us know!👇
© 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.