I hope this question has its place here.
I’ve always thought that connecting different Custom Post Types together is a rather common need, like in popular tutorial examples of custom taxonomy usage (books/authors/titles, films/actors/directors, etc). I personally use “Posts 2 Posts”, but support stopped.
On Google the most relevant results point to this plugin right now. Which long term alternatives exist? What would a seasoned WordPress web developer use to design a book/author/title management system with WordPress today, if P2P plugin is finished?
As a programmer, I would start writing my own code to connect my post types. It’s not a quick way, it’s not easy, but it’s a lot of fun.
The post types
We start by creating two simple post types,
Author
andBooks
:Nothing fancy there. In fact, it’s from the Codex!
The metabox
Let’s continue by adding a metabox for our author to our book post type:
Here you can see a callback function
p2p2_book_author_metabox
which is going to be what’s inside our metabox.The content of the metabox
Let’s create the function:
Here’s where the magic happens. First we’re going to query the database for authors and then we fill a
<select>
with our query results. Check the Codex for more aboutWP_Query
. Now you can go to your book post type and see your dropdown:Saving our content
Off course we want to save our selection so we add another function that’s going to save the metabox for us:
Now go and save a book with an author! The author of the book will be saved in the
wp_postmeta
database table and the selected value of the dropdown will be that in the meta.An author column for book
Let’s expand the admin area for our books. We will start by changing the columns:
This function makes sure we only see the columns title and p2p2_author. The cb checkbox column is needed for WordPress’ mass edit functionality. Now we need to add some information to our column. We add this function:
The switch is for every column you just added in the previous function. You fill it by echoing what you want to show. We get the post that’s the author of our book and create a nice permalink to his/her ‘profile page’. This is what it looks like:
To be continued
We connected two post types in the backend of our WordPress site, but we can’t see a thing of it in the frontend. It’ll need a lot more work to accomplish this, but the possibilities are somewhat endless. We could:
I will continue working on this answer, as I need this solution myself. However, I will stop working on in for now. I’ll start updating this answer tomorrow.
I wouldn’t worry about it too much, as a couple of great developers volunteered to continue support. However, if you want to use something else check out the ACF relationship field.
Since the original question is “What would you do if P2P went away?” I have a thought/suggestion. I actually needed, because if you’re building a plugin, telling users to install another plugin isn’t always viable.
One simple method would be to use the Post Meta. For example. In the post_meta of Author, you can store the books. Either as unique entries or a single comma separated entry or a serialized array. Then on the book, you store the inverse info of Author(s).
Another would be to add a new DB table (frowned upon) that stores the relationships and other pertinent information.
No idea how efficient either solution is at scale, but they work.