Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mishterk/24f5dc504eb99c93afc7bf03449f083c to your computer and use it in GitHub Desktop.
Save mishterk/24f5dc504eb99c93afc7bf03449f083c to your computer and use it in GitHub Desktop.
<?php
add_filter( 'acf/load_field/key=TARGET_FIELD_KEY_HERE', 'afp_test_acf_load_field_filter' );
function afp_test_acf_load_field_filter( $field ) {
$field['choices'] = [];
// Using get_posts() instead of WP_Query prevents query loop issues and global var overrides that break
// functionality on the advanced forms edit screen and potentially other contexts.
$posts = get_posts( [
'post_type' => 'post',
'posts_per_page' => - 1,
'orderby' => 'title',
'order' => 'ASC',
] );
foreach ( $posts as $post ) {
$field['choices'][ $post->ID ] = get_the_title( $post );
}
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment