wp-posts-to-posts
wp-posts-to-posts copied to clipboard
P2P and the WP REST API
Hi all,
I'm using the wp api to write my own app admin panel. I need the functionality that P2P offers in terms of creating groups of post types. However, since I'm using AngularJs to create the admin, I'm not entirely sure how I'd display, create, update and delete P2P relationships using js (angular in my case) and p2p.
I easily do it with posts using the wp REST API but how would I go about doing it with P2P relationships or is it even possible?
I'm year deep in JS, angular and wp so sorry if this is a dumb question.
Hallo, I too I'm looking at implementing WP REST API for P2P. I thing we have to extend the core api classes to create new endopints or whatever... I'm very new to the WP API thing so I have to study both P2P and the REST API, but I think this shuoldn't be too difficult. I'll post here something when I found some solution
I am able to create connections between 2 post types via the REST API. It took a custom endpoint with a custom callback, but relatively straightforward. Would be great to have this added to p2p core though ;)
@JiveDig Willing to share?
Certainly, it's a bit of a mess (lots of other code mixed in), but I'd love to get it refined so it's more re-usable. I'll try to post when i can come up for air.
I got it cleaned up and much more refined. It's an extendable class to create/delete connections via Rest API with minimal code needed per connection. http://cl.ly/102n0t1S3N2F
@JiveDig Would you mind sharing the code?
I plan too... i'm going to get it up in a repo with some decent docs so others can play (and hopefully improve). I'm using it on a big project now so i'm a bit crunched for time.
I'm trying to make p2p data show in rest api. did you by chance get your code up some place? or have any other incites?
Likewise, would love to see the code if possible! Thanks
Hey yall. I've actually updated/reworked it a couple times since i last posted. I wanted to make it into and extendable class, but failed miserably. Now i have it working as 1 class, and you put the connection name as a data-attribute in the link.
One question... if using this class (with helper function), would you rather enqueue your own JS file and use sample/example code, or have the class enqueue a file for you, and you just need to run a filter to define the location of that JS file?
Forgot to mention, the issue is that the js file not only needs to be enqueued but also localized. It's easier if the class does it, but less flexible if you wanted to add your own localization parameters. I could add a filter I guess.
If the end user needs to deal with the file and localization its way more steps and more room for ever, but also more flexible to those that know what they're doing.
Lastly, I could possibly make the whole thing into a plugin, then enqueue/localize, and deal with the whole JS file right in the plugin. There may be some issues in that, but I could try it if it seems the smartest route.
Okay, I opted to put this into a plugin. So far it's working really well. Simple to use and as powerful as Posts 2 Posts already is, just with AJAX connections ;)
Here's my post about it. http://thestizmedia.com/create-posts-2-posts-connections-wordpress-rest-api/
And here's the public repo. https://github.com/JiveDig/restful-p2p
Thanks JiveDig. I've just installed the plugin and it works perfectly.
Last week I did manage to use AJAX to do the same thing as this. Adding a user->post connection and removing it.
What I was really after was using the REST API from another website to make a connection.
So:
SiteA has p2p and this is where the connections are made SiteB - Press a button and create a connection between UserX and PostY
Any ideas on best way to approach that? I can currently create posts and users using the REST API, but wasn't sure how to create the connection between them.
Thanks a million!
Not sure I totally follow. You will have to keep users in sync on both sites?
I'm sure whatever you need to do is possible, but not something I currently have use for. I'd be curious to see it happen though ;)
Cool plugin, but how do you return p2p data via the REST API? Especially in a dynamic way?
@nickforddesign return how/for what? Right now, this is just for making p2p connections via ajax without refreshing the page.
@JiveDig Understood, but it looks like I'm not the only one who ended up in this thread looking for info on the first part of the querstion @beatelite asked:
I'm not entirely sure how I'd display, create, update and delete P2P relationships using js (angular in my case) and p2p.
I'm unsure about how to display the relationships. I know how to make custom REST API endpoints, but not sure how to make them dynamic (so they work for all my custom post types) with posts2posts...
@nickforddesign I guess i'm not fully understanding either, probably my fault. You can't display/create/update/delete p2p relationships using JS/ajax with the core P2P plugin. That's why I created restful p2p. That plugin lets you use a helper function to make it much easier to make those connections/relationships via JS. If you're a dev, maybe it could be a starting point for your own custom code.
Hi, I'm working on a another plugin to provide API routes and endpoints for p2p connection-types and connections.
Posts 2 Posts API Version 0.0.1 works, but doesn't have permission checks. So don't use it for production.
@raisonon You can do this with a POST
request to https://example-site.com/wp-json/p2papi/v1/connections
with arguments for p2p_from
(postId int), p2p_to
(postId int) and p2p_type
(p2pConnectionType string)
@nickforddesign You can display connections with a GET
request to https://example-site.com/wp-json/p2papi/v1/connections
or https://example-site.com/wp-json/p2papi/v1/connections/(?P<p2p_type>[a-zA-Z0-9-_]+)
.
The response includes the posts and connection ids, metadata, connection-type.
The plugin readme contains more information about routes for connections and connection-types.
any updates?
any updates?
Sorry, no update from my side. Didn't go on working on that plugin since my last post in 2018.
Would be interested, if anyone has any news.