Last active
May 6, 2020 15:30
-
-
Save timmcdaniels/13c2ff25f30196f524c4 to your computer and use it in GitHub Desktop.
Populating ACF Select Fields with Post Type Values
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
// populate acf field (sample_field) with post types (sample_post_type) | |
function acf_load_sample_field( $field ) { | |
$field['choices'] = get_post_type_values( 'sample_post_type' ); | |
return $field; | |
} | |
add_filter( 'acf/load_field/name=sample_field', 'acf_load_sample_field' ); | |
function get_post_type_values( $post_type ) { | |
$values = array(); | |
$defaults = array( | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC' | |
); | |
$query = new WP_Query( $defaults ); | |
if ( $query->found_posts > 0 ) { | |
foreach ( $query->posts as $post ) { | |
$values[get_the_title( $post->ID )] = get_the_title( $post->ID ); | |
} | |
} | |
return $values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice snippet of code. Problem i have is it won't allow to use the conditional logic for this field. Is there something new we have to do to enable that?