Home › Forums › ACF PRO › Custom Post Type Selection Question › Reply To: Custom Post Type Selection Question
Hi everyone. I’ve just worked in a solution for this issue thanks to a reply from James on a different question. I’ve created a set of custom fields on an options page where my user can create a custom header/footer to appear before/after a post type and had manually created a list of post types for them to choose from. This wasn’t great as the list would include assumed and potentially unused post types (like ‘product’) and not include post types added by plugins beyond my control.
I now hijack that select field and populate it with only active (and ‘public) post types with the following function:
function acf_load_post_type_choices( $field ) {
$field[‘choices’] = array();
$choices = get_post_types( array(‘public’ => true) );
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field[‘choices’][ $choice ] = $choice;
}
}
return $field;
}
add_filter(‘acf/load_field/name=post_types’, ‘acf_load_post_type_choices’);
Hope someone finds that useful.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.