A blog post can be anything you want. You could easily write one that links up a bunch of articles on your site. Titles, summaries, links… all hand-crafted HTML. A “guide”, as it were. It will likely be appreciated by your readers, I find, especially when you’re linking up old content that is still relevant and useful today.
But let’s say you want to programmatically create these “guides”. That might make them faster to create, easier to maintain, and give you a nice level of control over them. Let’s look at a way to do that.
Guides on CSS-Tricks
I’m writing about this because guides are something we’ve just started to do here on CSS-Tricks. For example, I wanted to make a guide of our content that is well suited for folks just starting out, so I made Our Guide To Just Starting Out with CSS & HTML.
That wasn’t built by hand, it was built by programmatically attaching a variety of content to a Custom Post Type we created just for Guides.
Programmatically Attaching Posts to Posts
You know how you can put images into blog posts? But in WordPress, there is also a concept of a featured image, which is one specific programmatically attached image for that post.
But we’re talking associating Posts with Posts not Images to Posts. There is no built-in WordPress way of doing that, so we’ll reach for plugins.
CMB2 and Friends
CMB2 (i.e. the second version of “Custom Meta Boxes”) is a free, open source plugin for adding better UI and functionality around custom fields. If you’re familiar with Advanced Custom Fields, it’s a bit like that, only I guess entirely free and a bit more modular.
With that installed, now you can install (I guess we’ll call them sub-plugins?) that make CMB2 do stuff. The one we’re after is CMB2 Attached Posts Field which has the explicit job of attaching Posts to Posts (or really, post type to any post type).
It gives you this two-column UI on post types you activate it for:
Move anything from the left to right, and it’s now programatically attached. This is exactly what we’re after. Now we can hand select, and hand order, any type of post to attach to any other.
Before you get to the UI you can see above, you not only need to install and activate those two plugins, but also tell CMB2 to create the custom meta boxes and apply them to the types of posts you want.
In our case, our Guides are a custom post type. That’s easy enough to enable: