Skip to content

Instantly share code, notes, and snippets.

@timmcdaniels
Last active May 6, 2020 15:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timmcdaniels/13c2ff25f30196f524c4 to your computer and use it in GitHub Desktop.
Save timmcdaniels/13c2ff25f30196f524c4 to your computer and use it in GitHub Desktop.
Populating ACF Select Fields with Post Type Values
// 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;
}
@robbiegod
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment