Last active
May 3, 2019 00:28
-
-
Save randyjensen/7305962 to your computer and use it in GitHub Desktop.
Advanced Custom Meta Fields Select Box Dynamically Created With a Looping Offset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment