Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active December 28, 2016 05:17
Show Gist options
  • Save kjbrum/5247e4b191480d21815e to your computer and use it in GitHub Desktop.
Save kjbrum/5247e4b191480d21815e to your computer and use it in GitHub Desktop.
Add custom options to an ACF field.
<?php
/**
* ACF Load Field Choices.
*
* @param array $field The field that we are going to add options to
* @return array $field The field we modified
*/
function acf_load_field_choices( $field ) {
// Reset choices
$field['choices'] = array();
// Get all the child pages
$child_pages = get_children( array( 'post_parent' => 1, 'post_type' => 'page' ) );
// Add our child pages as options
foreach( $child_pages as $child ) {
$field['choices'][$child->post_name] = $child->post_title;
}
return $field;
}
add_filter( 'acf/load_field/name=field_name', 'acf_load_field_choices' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment