Support

Account

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 );