CMB2 / cmb2-attached-posts

Custom field for CMB2 for attaching posts to a page.
GNU General Public License v2.0
135 stars 62 forks source link
cmb2 cmb2-attached-posts php wordpress

CMB2 Attached Posts Field

Custom field for CMB2.

The post IDs are saved in an array, which can be rearranged by dragging and dropping posts in the attached posts column. Now incorporates the same search field as the CMB2 Post Search field.

Installation

Follow the example in example-field-setup.php for a demonstration. The example assumes you have both CMB2 and this extension in your mu-plugins directory. If you're using CMB2 installed as a plugin, remove lines 6-9 of the example.

Customization

The example demonstrates how to modify the get_posts query args, and allows you to toggle the thumbnails display as well as a filter search input.

Usage

You can retrieve the meta data using the following:

$attached = get_post_meta( get_the_ID(), '_attached_cmb2_attached_posts', true );

This will return an array of attached post IDs. You can loop through those post IDs like the following example:

foreach ( $attached as $attached_post ) {
    $post = get_post( $attached_post );
}

Once you have the post data for the post ID, you can proceed with the desired functionality relating to each attached post.

Screenshots

  1. Field display
    Field display`

  2. Post search
    Post search`

Changelog

1.2.7

1.2.6

1.2.5

1.2.4

1.2.3

1.2.2

1.2.1

1.2.0