Choose custom post type from select/checkbox in a settings page

Support General Choose custom post type from select/checkbox in a settings page

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #4520
    agorskiagorski
    Participant
    
    $types = array();
    $post_types = get_post_types(array('_builtin' => false), 'object');
    foreach($post_types as $type){
        $types[$type->name] = $type->labels->singular_name;
    }
    
    $meta_boxes[] = array(
        'id'             => 'seasons',
        'title'          => __( 'Seasons', 'textdomain' ),
        'settings_pages' => 'tura-settings',
        'tab'            => 'seasons',
        'fields' => array(
            ...,
            array(
                'name'	=> __( 'Custom Post Types', 'textdomain' ),
                'id' 	=> 'primary_schedule_post_types',
                'type'	=> 'checkbox_list',
                'options' => $types,
            ),
            ...,
        ),
    );
    

    The checkbox list never displays any options. If I output the $types array, it comes up blank - [] - once, before displaying properly several more times.

    #4531
    Anh TranAnh Tran
    Keymaster

    Maybe your problem is the same as in this topic, where the post types are not registered when the code runs. You might want to do something like:

    add_action( 'init', 'prefix_register_post_types', 0 ); // 0 means high priority
    add_action( 'init', 'prefix_hooks', 5 ); // Hooks to 5 when all post types are registered. We should not hook to 10 or higher, because Meta Box already run at that time.
    function prefix_hooks() {
        add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
    }

    The problem comes when I added compatibility for WPML.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Choose custom post type from select/checkbox in a settings page’ is closed to new replies.