• Resolved Laura Yeffeth

    (@clampdesign)


    Thanks so much for your work on this plugin. I started with version 2.0.0.0 and have been upgrading versions along with the development but I noticed an issue when going from version 2.0.0.4 to 2.0.0.6 (I must have skipped 2.0.0.5). I have a select field that is pulling in custom posts. This worked perfectly in version 2.0.0.4, but since upgrading to 2.0.0.6, all the select options appear as Array.

    This is my metabox code:

    $prefix = '_cmb2_';
    
    $meta_boxes['related_books'] = array(
        'id'            => 'related_books',
        'title'         => __( 'Related Books', 'cmb2' ),
        'object_types'  => array( 'book', ),
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true,
        'fields'        => array(
          array(
            'name' => __( 'Heading', 'cmb2' ),
            'id'   => $prefix . 'related_books_heading',
            'default' => 'You may also like',
            'type' => 'text',
          ),
          array(
              'name'    => __( 'Select First Related Book', 'cmb' ),
              'id'      => $prefix . 'related_book__1',
              'type'    => 'select',
              'options' => cmb_get_post_options(),
          ),
          array(
              'name'    => __( 'Select Second Related Book', 'cmb' ),
              'id'      => $prefix . 'related_book__2',
              'type'    => 'select',
              'options' => cmb_get_post_options(),
          ),
          array(
              'name'    => __( 'Select Third Related Book', 'cmb' ),
              'id'      => $prefix . 'related_book__3',
              'type'    => 'select',
              'options' => cmb_get_post_options(),
          ),
        ),
      );

    and this is the function to pull in the posts:

    function cmb_get_post_options( $query_args ) {
    
          $args = wp_parse_args( $query_args, array(
              'post_type' => 'book',
              'numberposts' => -1,
              'orderby' => 'title',
              'order' => 'ASC',
            ) );
    
          $posts = get_posts( $args );
    
          $post_options = array('Select Book');
          if ( $posts ) {
            foreach ( $posts as $post ) {
             $post_options[] = array(
               'name' => $post->post_title,
               'value' => $post->ID
             );
            }
          }
    
          return $post_options;
      }

    Is there a different format that the function needs to return for this to work now?

    At the moment I am working locally (I reverted back to version 2.0.0.4 for the site that is on my server as my client is working with the site for a launch on the 5th) but I can upgrade to the version with the issue and give you access if you need to take a look. I appreciate the help.

    https://www.ads-software.com/plugins/cmb2/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    Yes, selects/radios/etc now expect a key => value pair. See example below.

    function cmb_get_post_options( $query_args ) {
    
    	$args = wp_parse_args( $query_args, array(
    		'post_type' => 'book',
    		'numberposts' => -1,
    		'orderby' => 'title',
    		'order' => 'ASC',
    	) );
    
    	$posts = get_posts( $args );
    
    	$post_options = array('Select Book');
    	if ( $posts ) {
    		foreach ( $posts as $post ) {
    			$post_options[ $post->ID ] = $post->post_title;
    		}
    	}
    
    	return $post_options;
    }

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Oops! I wrote that incorrectly. (but re-edited the post to correct it)

    Thread Starter Laura Yeffeth

    (@clampdesign)

    This is perfect. Thank you for such a quick response!

    Thread Starter Laura Yeffeth

    (@clampdesign)

    Forgot to mark as resolved. Thanks again!

    Thanks for this function \o/

    But in debug, show this warning:

    Warning: Missing argument 1 for cmb_get_post_options(), called in
    ...
    on line 41

    Notice:

    Notice: Undefined variable: query_args in
    ...
    on line 43

    How to fix it?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Angelo, just to confirm, are you using it with a select field?

    Yes.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Sounds like for some reason, the $query_args variable from Justin’s example isn’t getting properly passed in, or perhaps this is running before the variable is defined.

    Thread Starter Laura Yeffeth

    (@clampdesign)

    I am suddenly having this problem now too.

    Warning: Missing argument 1 for cmb_get_post_options()

    Also, I have WP_DEBUG set to false but these errors are showing up on the front of my site! I need to get this fixed ASAP. Any idea how to correct this issue?

    Not the best solution, but i set an empty parameter in the function cmb_get_post_options().

    'options' => cmb_get_post_options(''),
    Thread Starter Laura Yeffeth

    (@clampdesign)

    Thanks, Angelo!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Problem using select field of custom posts from version 2.0.0.4 to 2.0.0.6’ is closed to new replies.