CMB2 creates metaboxes and forms with custom fields that will blow your mind. Attached Posts in an extension of this extremely useful plugin.
Every once in a while you may find yourself needing to attach various pieces of content to various other pieces of content within your site. Maybe you want to display a list of related content on a single post but you’re extremely particular about which posts should display as related. Maybe you have a special page template which needs to display various posts as sections or chapters on a single page.
Whatever the case may be, we always seem to find ourselves needing to attach one type of content to another type of content. With CMB2 Attached Posts, we give you that ability.
Sure, there are plugins that will do this for you. Here at WebDevStudios we’ve used Posts 2 Posts quite a bit which is a wonderful plugin allowing you to pair content from one post type to another. Why use CMB2, then? Well, why not?? We want to make CMB2 be and do all that it can, and when the need for relationships between post types came up it seemed like the perfect time to integrate this functionality directly into CMB2.
First things first, you’ll need to check out the CMB2 Attached Posts repo. You’ll drop those files right into your /mu-plugins/ directory, which will register the new CMB2 field type and enqueue the necessary JavaScript and CSS files in the dashboard.
So now that you’ve got that taken care of, let’s go!
First, we need to add the field like we would with any other CMB2 field:
As an added bonus, our own Justin Sternberg just made an update to this functionality on line 15 above. You can now specify the number of posts you want to pull in when using this field type!
Once you’ve got all of your options and parameters set, you’ll wind up with something like this:
Simple, right? This is going to look for posts and display this field ONLY on pages as indicated by the ‘object_types’ value above.
Once you have that in place, you’re ready to start draggin’ and droppin’!
When you update the post you’re editing, you’ll now have these post IDs saved in an array for you to use on the front-end. By rearranging posts, you’re able to rearrange them in the array itself; so, if you need to display this content in a specific order then drag and drop is going to be your best buddy.
Now that you have a nifty little array, you can do something like this do bring these babies to life:
The array stores the ID of each post, so the world is really your stinky little oyster as far as what you want to do with these posts. It’s all up to you!
You can grab CMB2 here: https://github.com/WebDevStudios/CMB2
You can grab CMB2 Attached Posts here: https://github.com/WebDevStudios/cmb2-attached-posts/
Love CMB2 more and more guys. It saves me HOURS of work, every day.
Attaching posts ( and a few other things ) are definitely on my list of desirables.
Thanks so much for the kind words! We’re huge fans of CMB2 here as well and always looking for ways to make it better. So glad you’re getting so much use out of it!
Thanks, this is great. But how do I link custom posts instead of regular posts? I was able to show the metabox on a certain custom post type, but I was not able to link anything other than regular posts.
On line 41 of cmb2-attached-posts-field.php (https://github.com/WebDevStudios/cmb2-attached-posts/blob/master/cmb2-attached-posts-field.php#L41), you can specify the post type. When you create your custom field type for CMB2, you can change this value (and other values set in get_posts) to change the output of your new attached posts field.
Thanks for reading and using CMB2!
Thanks, that worked! I have another question, it’s related to WP Query.
I want to filter the posts that are showing inside the admin so that the author of the custom posts being retrieved is the same as the author of the current post being shown in the admin. I don’t want all the posts being returned.
I know I have to add the author parameter to WP query in /cmb2-attached-posts-field.php. I’m just not sure how to do the query so that the author matches the author of the post being edited.
Any help will be appreciated.
I have been using posts 2 posts.
It’s not clear that I can attach a custom post type A to custom post type B.
Is that set up in the “‘object_types’ => array( ‘page’ ),” line.
Does this field know only to relate one CPT to another if there are two CPTS listed here?
Hi,
I am very satisfied with CMB2 which I use in all my projects. I recently worked on an international project and so I have to use WPML. I’d like to know if there is such compatibility as provided for qtranslate ? Also whether it is intended to synchronize data between different languages ?
Thanks a lot,
Léo Graph.
This is definitely more of a question for core CMB2 than this add-on specifically. For questions about the core functionality of CMB2, you can always post an issue on the Github page: https://github.com/WebDevStudios/CMB2/issues
Fine, thank you for answering. I will submit that if it is not already done 🙂
Léo Graph.
Hello,
I asked on the synchronization of data (meta) between languages on Github. I have extensively tested CMB2 which is fully compatible with WPML, but not this extension.
However, to be, just add “‘suppress_filters’ => 0” following the line 43 of file “CMB2-attached-posts-master.php” ; posts shown in the list will be in the right language .
Now, It’s just missing synchronization between language, to have an equivalent extension to the famous 2-Posts Posts.
Best regards.
Hey Corey Collins,
I love CMB2 and I use it a lot. I now found Attaching posts, and it looks very nice.
The only problem I don’t see any “Available Posts” nor “Attached posts” and the description: “Drag posts from the left …..” is also missing.
I only see the title “Attached Posts” and nothing else in the box?!
(just using the example-field-setup.php)
What could be the problem. I am lost?!
Thanks for any suggestions.
With this post being about 2 years old at this point, it’d be a safer bet to follow along with this stuff over at Github: https://github.com/WebDevStudios/cmb2-attached-posts
This has been updated pretty extensively over the past couple of years and would have the most up-to-date information for you.
Hi,
I am very satisfied with CMB2. But the “show_on” filter doesn’t work in attaching posts with CMB2. It seems that it’s not included in your add-on.
How can I do to show attaching posts in specific page template ?
Best regards.
It looks like you posted this issue over at the CMB2 Attached Posts Github which is awesome!
Link here: https://github.com/CMB2/cmb2-attached-posts/issues/45
I think that will be the best place to look for updates on this.
Thanks!
Hi, I’m using this plugin with a multilanguage site, I’m using Polylang plugin, but when I want to attach post alway show default language. The post dont filter by language. Do you have any advice for this.
Thanks
Hi Juan – with this being such an old post at around 8 years old, and with not using CMB2 anymore, I’m not sure how well this will or won’t work for your site.
If you’d like to create an issue in Github, perhaps the team will be able to look into it and see if it’s something that can be updated in the plugin: https://github.com/CMB2/cmb2-attached-posts/issues
Thanks!