Tagged: post-to-post relationships, wp rest api
- This topic has 18 replies, 2 voices, and was last updated 6 years, 1 month ago by
Jason.
-
AuthorPosts
-
-
December 19, 2015 at 3:42 pm #5383
friendlyfire3MemberI have some CPT relationships that need to happen and I see that piklist (in all it’s brilliance) has the ability to create these: https://piklist.com/user-guide/docs/fields/post-post-relationships/
However, I’m not using the wp admin for linking the posts themselves. I need to use a custom built angualrJs app that manages a lot of wp admin functionality via the wp rest api.
I will set up the relationships via piklist but what would be the best way of going about exposing the ability to link posts to other posts via the rest API?
Would I just need to create a new endpoint for each CPT? I’ve never created one so if you have any suggestions on it the best approach, I’d appreciate it.
I realize this is out of scope of your support but thought I’d ask for a general direction to get me started.
Thanks again for everything~
-
December 19, 2015 at 4:35 pm #5384
friendlyfire3MemberAlso, I heard that the post-to-post relationships was going to be overhauled. Is that done or still in the works?
-
December 21, 2015 at 12:05 pm #5387
JasonKeymasterHey friendlyfire3!
There’s actually a couple different overhauls in the works. First, if you’re using the trunk, the relate data is no longer held in a separate wp_relationships table, but now in the respective meta tables. With that, the relationships were also opened up beyond just post-to-post; it’s now possible to relate users, posts, and comments in any combination (e.g. user-to-user, comment-to-comment, post-to-user, etc.).
Next, not available yet but was just finished internally, we finished a relate_query system (much like meta_query) that will support any combination of conditions. I’m pretty excited about it. 🙂
As for your scenario, I assume you’re using the WP-API v2 plugin? Keep in mind that WP 4.4 only has the core for the api, and none of the endpoints, yet. From there, all you need to do is provide the post_has/post_belongs arguments to the filter in the endpoint. Whatever you put in there is passed directly to the wp_query, like you would normally use.
Hope that helps!
-
December 21, 2015 at 1:23 pm #5389
friendlyfire3Memberomg. Mind blown. I cannot wait to see your work on the relationship query. Any ideas of release timeline? Q1, Q2?
Yes, I’m using the V2 api plugin (beta9) and I’m using piklist 0.9.4.29.
Also, just to clarify the filter: If I wanted to get the posts related to post with an id of 1, would this work? site.dev/wp-json/wp/v2/posts/1?filter[post_has]
I just tried that on my dev site and I’m not seeing them in the response even though I double checked that post 1 has other posts selected.
What am I missing?
-
December 21, 2015 at 3:36 pm #5390
JasonKeymasterI’m not the one that sets the timelines, but I believe it’s safe to say Q1. Still, don’t hold me on that, just take it with glib optimism. 🙂
Whenever you use the id endpoint, the api simply grabs the post with that id, no questions asked. It always returns one post. So whenever you want one or more posts, use the /wp/v2/posts/ endpoint, as that’s the equivalent of using get_posts (which is where post_has/belongs are useable).
So it would be something like this: site.dev/wp-json/wp/v2/posts?filter[post_has=1]
Make sense?
-
December 21, 2015 at 4:27 pm #5391
friendlyfire3MemberNp on the timeline. I know how these things go.
Are you sure about the url? I just tried it and it returns all posts from the site.
-
December 21, 2015 at 5:07 pm #5396
JasonKeymaster@friendlyfire3: I’m sure about the endpoint. Try using a standard wp_query argument (e.g. post__in) in the filter to make sure filter is working as expected. If another argument is working fine, let me know.
-
December 21, 2015 at 6:01 pm #5398
friendlyfire3MemberYes, when I use
http://site.dev/wp-json/wp/v2/posts?filter[category_name]=testcatI get only the posts back that have the ‘testcat’ assigned to it.
When I try
http://site.dev/wp-json/wp/v2/posts?filter[post_has]=1(I tried your way of [post_has=1] and notta)I get back the 10 latest posts from the site (none of which are related to post 1)
I’m authenticated on all requests.
Is this an api issue you think?
-
December 21, 2015 at 10:09 pm #5399
JasonKeymasterHey friendlyfire3!
Interesting. I’m wondering if the api isn’t loading Piklist. Admittedly I haven’t tested this yet, and I won’t be able to write any proper unit tests until the standard endpoints are written in (I believe) 4.5.
Can you please try a couple things for me?
First, see if this works:
http://site.dev/wp-json/wp/v2/posts?filter[suppress_filters]=0&filter[post_has]=1If that doesn’t work, please try adding this to your theme/plugin:
add_filter('rest_query_vars', function($valid_vars) { $valid_vars[] = 'post_has'; $valid_vars[] = 'post_belongs'; return $valid_vars; });I’m thinking the api isn’t applying the arguments are they’re not being permitted.
Hope this helps!
-
December 21, 2015 at 10:22 pm #5400
friendlyfire3MemberPerfect! had to drop the php function in the plugin and now everything’s working perfectly!
Awesome work. Can’t wait to play around.
Thanks a mill, Jason.
-
December 21, 2015 at 10:29 pm #5401
JasonKeymasterHey friendlyfire3!
Awesome! I’m glad that worked! That code won’t be necessary in the next version of Piklist, so let’s modify it so you don’t have any issues when upgrading Piklist in the future:
add_filter('rest_query_vars', function($valid_vars) { return in_array('post_has', $valid_vars) ? $valid_vars : array_merge($valid_vars, array('post_has', 'post_belongs'); }); -
December 21, 2015 at 10:35 pm #5402
friendlyfire3MemberOne more question: What if I wanted to POST to this?
I want to be able to select the posts from the angular app to relate to a post so I’d obv need to send a post request.
Do I need to send something special to the regular posts endpoint? I assume I can’t just post to the filter.
-
December 21, 2015 at 10:43 pm #5403
JasonKeymasterHmm.. So you’re hoping to create the relationships yourself between two posts? I want to make sure I understand you correctly. You’d have an “active post” of some sort, then select other posts that it’s related to, and somehow apply the new relationships via the API?
-
December 21, 2015 at 10:55 pm #5404
friendlyfire3Memberyes, exactly what I want to do.
I have a post that i’m editing. I’ll expose the relationship to other posts (via piklist) and then I want to select those and submit as a POST request. Then I’ll use the filter you showed me to show what’s already connected when viewing the post.
-
December 22, 2015 at 10:26 am #5406
JasonKeymasterHey friendlyfire3!
Hmm.. I’m afraid this puts you into a bit of a more complicated issue. You see, the GET endpoint you’re using uses the wp_query, which Piklist hooks into. So it’s a simple matter of using the post_has/belongs arguments in the query, and you’re good to go.
Creating a relation, on the other hand, is unfortunately more complicated, and in more than one way. Like I said earlier, in the version you’re using Piklist stored the relationships in a wp_relationships table. But, since then, it’s changed to the respective meta tables. Now, if you upgrade from one to the next, there’s an upgrade script that runs and stores all your existing relationships into the new form — nothing lost.
So here’s your options to get this working:
- First, keep your current version of Piklist and create a custom POST endpoint that receives two post ids — the owner and one being owned, and have it use the Piklist_Form::relate function to make the relationship. The biggest downside here is that when you do upgrade to a newer version of Piklist, you’ll have to change your code to the next option.
- Second, upgrade to the latest in trunk (which is in beta), and use the post meta endpoint to add the relationships (until we’re able to add some of our own custom endpoints). To create a relationship, you simply add a meta value to the post that <u>belongs</u> to the other. The key would be
__post_relateand the value would be the id of the owner. Simple as that.
Glad to see that you’re pushing Piklist to what it can do. Sorry we’re not quite caught up with API stuff, yet. 😀
-
December 22, 2015 at 10:59 am #5408
friendlyfire3MemberThanks for this. I’ll give it a go and link to the github page for you or others to refer to.
Thanks for sticking this out with me, Jason. I know it’s an edge case.
-
December 22, 2015 at 11:52 am #5409
JasonKeymasterSure! It’s an edge case. But it’s a cool one. 😉
-
December 22, 2015 at 3:44 pm #5416
friendlyfire3MemberLast question while I’ve got you – since you seem to be the expert on relating:
Will there ever be reciprocal connections? I can link a post to another – which is great. What about the option of two-way relating without having to do it manually via the other post.
-
December 22, 2015 at 3:59 pm #5417
JasonKeymasterUnless I misunderstand you, that’s already happening.
Let’s say I have two posts types, the standard blog post type, and an event post type. Now let’s say I want to be able to mark related blog posts whenever I create an event. What we have now is that for every event it has one or more posts; conversely, a given post belongs to one or more events.
So given that, you would retrieve in each direction like this:
// Note: suppress_filters needs to be false as by default get_posts suppresses custom filters $related_posts = get_posts(array( 'suppress_filters' => false 'post_type' => 'post', 'post_belongs' => $event_id )); $related_event = get_posts(array( 'suppress_filters' => false 'post_type' => 'event', 'post_has' => $post_id ));Is this what you’re talking about?
-
-
AuthorPosts
- You must be logged in to reply to this topic.