name = 'post_type_selector'; $this->label = 'Post Type Selector'; $this->category = 'relational'; } public function render( $field, $value = '', $name = null ) { $name = $name ?: "metaforge[{$field['name']}]"; $appearance = $field['appearance'] ?? 'select'; // Backward compatibility for the old 'multiple' toggle if ( isset( $field['multiple'] ) && empty( $field['appearance'] ) ) { $appearance = ! empty( $field['multiple'] ) ? 'multi_select' : 'select'; } $is_multiple = in_array( $appearance, array( 'checkbox', 'multi_select' ) ); $name_attr = $is_multiple ? $name . '[]' : $name; $val_array = is_array( $value ) ? $value : (array) $value; // Fetch public post types but exclude 'attachment' (Media library) $post_types = get_post_types( array( 'public' => true ), 'objects' ); if ( isset( $post_types['attachment'] ) ) { unset( $post_types['attachment'] ); } echo '
'; echo ''; echo '
'; if ( $appearance === 'checkbox' || $appearance === 'radio' ) { $list_class = $appearance === 'checkbox' ? 'mfe-choice-list mfe-checkbox-list' : 'mfe-choice-list mfe-radio-list'; echo '
'; $type = $appearance === 'checkbox' ? 'checkbox' : 'radio'; foreach ( $post_types as $pt ) { $is_selected = in_array( $pt->name, $val_array ); echo ''; } echo '
'; } else { $placeholder = '— Select ' . esc_html( $field['label'] ) . ' —'; if ( $appearance === 'multi_select' ) { echo ''; echo ''; } foreach ( $post_types as $pt ) { $is_selected = in_array( $pt->name, $val_array ); echo ''; } echo ''; } echo '
'; echo '
'; } public function sanitize( $value, $field ) { if ( is_array( $value ) ) { return array_map( 'sanitize_key', $value ); } return sanitize_key( $value ); } }