The Stiz Media, LLC

Making your website work for you

  • Styles for Genesis
    • FAQs
  • Cart
  • My Account
  • Home
  • Web Design
  • Portfolio
  • Resources
  • Blog
  • Contact

Powerful websites, with style.

Home » Blog » Create Posts 2 Posts ‘connections’ with the WordPress Rest API

Create Posts 2 Posts ‘connections’ with the WordPress Rest API

August 28, 2016 Andhi 9 Comments

Restful P2P Example

I’ve written a lightweight plugin to make it extremely easy to create Posts 2 Posts connections via the WordPress WP-API (Rest API). If you don’t know about Posts 2 Posts, it’s an extremely powerful (developer) plugin that allows you to create a ‘connection’ between 2 objects, often posts, pages, or CPT, but also users.

A typical example Posts 2 Posts (P2P) usage is a website where you have 2 custom post types, actors and movies. They exist as separate post types so they can easily be managed via the WP backend, and each post type can conveniently have its own set of custom fields associated with it. P2P allows you to ‘connect’ (one-to-many or many-to-many) an actor to the movies they were in, or vice-a-versa (optional).

Lately I’ve been using P2P for more user based connections like allowing a logged in user to ‘like’ or ‘favorite’ posts, mark a CPT as ‘completed’, or save posts/CPT’s to ‘wish lists’. When I think about the use cases for allowing connections between logged in users and a CPT, the ideas start flowing.

Using the WordPress Rest API allows the power and flexibility of P2P connections with the ease of the WP-API for creating the connections (front end, ajax = no page reload, woot!).

Restful P2P example

The Restful P2P plugin comes with a simple helper function to help you create/display a connection button.

That’s it. Everything else is handled for you. Nice and tidy.

Now is your turn. Give the Restful P2P plugin a try.

What are some use cases you have for a plugin like Restful P2P and Posts 2 Posts?

Related Posts

  • CPT filter by taxonomy
    Custom Post Type Filter Admin By Custom Taxonomy
  • Remove Items from TinyMCE editor
    Remove Buttons/Items From The WordPress TinyMCE Editor
  • EA Share Count SMS button
    Add SMS share button to EA Share Count plugin

Tutorials Posts 2 Posts

‹ Add SMS share button to EA Share Count plugin

Try The Most Powerful Genesis Child Theme Ever Created

Mai Theme is the most customizable Genesis child theme (and plugin!) available. Check it out now!

Learn More @ MaiTheme.com

Our posts, directly to your inbox

Comments

  1. Luke Cavanagh says

    August 29, 2016 at 12:15 pm

    Thanks for sharing!

    Reply
    • JiveDig says

      August 29, 2016 at 12:28 pm

      Thanks for checking it out!

      Reply
  2. Kevin says

    November 29, 2016 at 11:31 am

    Thanks for sharing JiveDig. Your plugin has already come is very useful on a project that I’m working on right now. This is my first foray into the WP REST API and it’s a steep learning curve but I’ve made some modifications to your code in order to use p2p as a one-directional ‘friend’ system where some users can send requests to other users who are in turn invited to ‘approve’ or decline’. I’ve done this by introducing a new variable of ‘request’ with arguments connect (default), request, accept, decline which attribute some meta to the connection.

    My question is how might I extend your class to include a p2p_update_meta function that works in the same way as the connect and disconnect functions but only updates the meta? The update meta documentation on the p2p site is pretty light.

    PS: You mention at the foot of the repo that you might remove the option to disconnect as you don’t see a use case for it. Well, I’m also using your plugin to allow users to register (and unregister) for events so it’s certainly useful in this case.

    Thanks again
    Kevin

    Reply
    • JiveDig says

      January 9, 2017 at 9:48 am

      Hey Kevin, we touched base already but just replying here for anyone that is interested. It would be cool to add p2p meta support to this plugin. I haven’t needed it yet, but it shouldn’t be too difficult to add another parameter.

      As per removing the option to disconnect, I mean to make it optional. Then we can create connection buttons that are only for connecting or both connecting and disconnecting.

      Thanks for checking it out!

      Reply
  3. Sam Cohen says

    May 19, 2017 at 1:16 pm

    This is really good to know!

    P2P can actually make your posts:

    favorited,
    saved
    checklisted

    It would nice to have a write-up tut on these!

    Reply
    • JiveDig says

      July 10, 2017 at 10:48 am

      Agreed. That was the goal of this plugin, to make it really easy to create Favorites, Saved, Bookmarked, etc posts/CTP’s.

      Reply
      • BBT says

        November 5, 2019 at 2:17 pm

        Wow, this is fantastic. Thanks for making this available. I’ve integrated this already so that users can favorite stuff on my site. But how would one do this for different sets of lists, like favorites, bookmarks, save for later etc.? This is just a single connection right? Is it possible to distinguish each connection for different purposes?

        Thank you

        Reply
        • JiveDig says

          November 5, 2019 at 2:42 pm

          Posts 2 Posts isn’t really supported anymore which is sad. But it allows you to create connections between almost anything. The “name” parameter of my code is the connection to use, but you can register a posts 2 posts connection between any object types.

          Reply
          • BBT says

            November 11, 2019 at 3:05 pm

            Thanks for the tip even though you’re not supporting this anymore. I just restarted a side project I began several years ago but p2p still functions perfect, I appreciate the work you’ve done on this in the past. 

            If I may though, I have another question, was there a way to query for posts in order based on how many user connections a post has? To show the most popular posts on the homepage? If not I was thinking I should add a custom meta field to all posts and add a hook that increments and decrements the value with each connection. Thanks for any thoughts. 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Our posts, directly to your inbox


Howdy, I'm Mike Hemberger. I like to build nice websites and web apps with WordPress and Genesis Framework.

I'm passionate about everything I do, and it resonates in all of my work.

Feel free to email, and you'll be speaking directly with me.

Recent Posts

Restful P2P Example

Create Posts 2 Posts ‘connections’ with the WordPress Rest API

EA Share Count SMS button

Add SMS share button to EA Share Count plugin

ACF Extras

ACF Extras officially launched! A community of code snippets and field groups

Remove Items from TinyMCE editor

Remove Buttons/Items From The WordPress TinyMCE Editor

  • My Account
  • Affiliate Area
  • Log In|Log Out
  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright © 2025 · The Stiz Media, LLC · All Rights Reserved