Viewing 18 reply threads
  • Author
    Posts
    • #5383

      I 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~

    • #5384

      Also, I heard that the post-to-post relationships was going to be overhauled. Is that done or still in the works?

    • #5387
      Jason
      Keymaster

      Hey 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!

    • #5389

      omg. 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?

    • #5390
      Jason
      Keymaster

      I’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?

    • #5391

      Np 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.

    • #5396
      Jason
      Keymaster

      @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.

    • #5398

      Yes, when I use http://site.dev/wp-json/wp/v2/posts?filter[category_name]=testcat

      I 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?

    • #5399
      Jason
      Keymaster

      Hey 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]=1

      If 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!

    • #5400

      Perfect! 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.

    • #5401
      Jason
      Keymaster

      Hey 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');
      });
    • #5402

      One 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.

    • #5403
      Jason
      Keymaster

      Hmm.. 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?

    • #5404

      yes, 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.

    • #5406
      Jason
      Keymaster

      Hey 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_relate and 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. 😀

    • #5408

      Thanks 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.

    • #5409
      Jason
      Keymaster

      Sure! It’s an edge case. But it’s a cool one. 😉

    • #5416

      Last 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.

    • #5417
      Jason
      Keymaster

      Unless 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?

Viewing 18 reply threads
  • You must be logged in to reply to this topic.