Skip to content

Conversation

@mclaurent
Copy link

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.
		),
	) );
````

…cted.

Simply add a data attribute and enter the desired number of items:
attributes' => array(
   'data-max-items' => 3,
),
@mclaurent mclaurent changed the title Adding support for limiting the amount of post types that can be sele… Adding support for limiting the amount of posts that can be sele… Mar 6, 2018
@mclaurent mclaurent mentioned this pull request Jun 21, 2018
BoCarlson added a commit to BoCarlson/cmb2-attached-posts that referenced this pull request Dec 19, 2019
templeman added a commit to modernadventure/cmb2-attached-posts that referenced this pull request Oct 19, 2020
@kkoppenhaver
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants