cmb2-attached-posts icon indicating copy to clipboard operation
cmb2-attached-posts copied to clipboard

Adding support for limiting the amount of posts that can be sele…

Open mclaurent opened this issue 7 years ago • 1 comments

Here's a PR that will introduce the functionality to specify a max amount of posts to be attached. The user won't be able to add more than X posts into your metafield. To specify how many items are allowed, add a data attribute "max-items" to your metafield If the "max-items" field isn't specified, then unlimited items are allowed (as it would without this change). For example:

/**
 * Define the metabox and field configurations.
 *
 * @param  array $meta_boxes
 * @return array
 */
function cmb2_attached_posts_field_metaboxes_example() {
	$example_meta = new_cmb2_box( array(
		'id'           => 'cmb2_attached_posts_field',
		'title'        => __( 'Attached Posts', 'yourtextdomain' ),
		'object_types' => array( 'page' ), // Post type
		'context'      => 'normal',
		'priority'     => 'high',
		'show_names'   => false, // Show field names on the left
	) );
	$example_meta->add_field( array(
		'name'    => __( 'Attached Posts', 'yourtextdomain' ),
		'desc'    => __( 'Drag posts from the left column to the right column to attach them to this page.<br />You may rearrange the order of the posts in the right column by dragging and dropping.', 'yourtextdomain' ),
		'id'      => 'attached_cmb2_attached_posts',
		'type'    => 'custom_attached_posts',
		'column'  => true, // Output in the admin post-listing as a custom column. https://github.com/CMB2/CMB2/wiki/Field-Parameters#column
		'options' => array(
			'show_thumbnails' => true, // Show thumbnails on the left
			'filter_boxes'    => true, // Show a text box for filtering the results
			'query_args'      => array(
				'posts_per_page' => 10,
				'post_type'      => 'page',
			), // override the get_posts args
		),
		attributes' => array(
		   'data-max-items' => 3, //change the value here to how many posts may be attached.
		),
	) );
````

mclaurent avatar Mar 05 '18 17:03 mclaurent

Is this something that's ready to be merged? I was looking into the documentation and could really use this on a current project. Happy to help test, write docs or help with anything that's holding this up. Thanks in advance!

kkoppenhaver avatar Mar 07 '23 13:03 kkoppenhaver