Skip to content

Commit 2e834ea

Browse files
committed
Merge pull request #12 from mmcachran/master
fix trying to get property of non-object errors when using multiple post types
2 parents 7e1293c + c807652 commit 2e834ea

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

cmb2-attached-posts-field.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,25 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
4343
'order' => 'ASC',
4444
) );
4545

46-
// Get post type object for attached post type
47-
$attached_post_type = get_post_type_object( $args['post_type'] );
46+
// loop through post types to get labels for all
47+
$post_type_labels = array();
48+
foreach ( (array) $args['post_type'] as $post_type ) {
49+
// Get post type object for attached post type
50+
$attached_post_type = get_post_type_object( $post_type );
51+
52+
// continue if we don't have a label for the post type
53+
if ( ! $attached_post_type || ! isset( $attached_post_type->labels->name ) ) {
54+
continue;
55+
}
56+
57+
$post_type_labels[] = $attached_post_type->labels->name;
58+
}
59+
60+
$post_type_labels = implode( '/', $post_type_labels );
4861

4962
// Check 'filter' setting
5063
$filter_boxes = $field->options( 'filter_boxes' )
51-
? '<div class="search-wrap"><input type="text" placeholder="' . sprintf( __( 'Filter %s', 'cmb' ), $attached_post_type->labels->name ) . '" class="regular-text search" name="%s" /></div>'
64+
? '<div class="search-wrap"><input type="text" placeholder="' . sprintf( __( 'Filter %s', 'cmb' ), $post_type_labels ) . '" class="regular-text search" name="%s" /></div>'
5265
: '';
5366

5467
// Get our posts
@@ -70,7 +83,7 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
7083

7184
// Open our retrieved, or found posts, list
7285
echo '<div class="retrieved-wrap column-wrap">';
73-
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Available %s', 'cmb' ), $attached_post_type->labels->name ) . '</h4>';
86+
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Available %s', 'cmb' ), $post_type_labels ) . '</h4>';
7487

7588
// Set .has_thumbnail
7689
$has_thumbnail = $field->options( 'show_thumbnails' ) ? ' has-thumbnails' : '';
@@ -107,7 +120,7 @@ public function render( $field, $escaped_value, $object_id, $object_type, $field
107120

108121
// Open our attached posts list
109122
echo '<div class="attached-wrap column-wrap">';
110-
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Attached %s', 'cmb' ), $attached_post_type->labels->name ) . '</h4>';
123+
echo '<h4 class="attached-posts-section">' . sprintf( __( 'Attached %s', 'cmb' ), $post_type_labels ) . '</h4>';
111124

112125
if ( $filter_boxes ) {
113126
printf( $filter_boxes, 'attached-search' );

0 commit comments

Comments
 (0)