Support

Account

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.