Shoora
5/3/2019 - 12:28 AM

Advanced Custom Meta Fields Select Box Dynamically Created With a Looping Offset

Advanced Custom Meta Fields Select Box Dynamically Created With a Looping Offset

function my_acf_load_field($field) {
	$field['choices'] = array();

  	$post_type = 'xxxPOST-TYPE-NAMExxx'; // post type name
  	$acf_field_id = 'xxxACF-FIELD-IDcxxx'; //eg: field_52129dt7c49dd
	$global_offset = 500; // whatever you want your looping offset to be
	$num_props = wp_count_posts($post_type)->publish;
	$num_props_pages = (intval($num_props/$global_offset)) + 1;
  	$offset_props = 0;

	while ($num_props_pages >= 0) {
	  	$choices = new WP_Query(
	  		array(
	  			'post_type' => $post_type, 
	  			'posts_per_page' => $global_offset, 
	  			'offset' => $offset_props
	  		)
	  	);

		if ($choices->have_posts()) {
			foreach ($choices->posts as $choice) {
				$value = get_sub_field('value');
				$label = get_sub_field('label');
	
				$field['choices'][$choice->ID] = get_field($acf_field_id, $choice->ID) . ' (Property ID: ' . $choice->post_name . ')';	
			}
		}
		wp_reset_query();
		$offset_props = $offset_props + $global_offset;
		$num_props_pages--;
	}

    return $field;
}
add_filter('acf/load_field/key='$acf_field_id, 'my_acf_load_field');