- This topic has 6 replies, 2 voices, and was last updated 3 years, 7 months ago by
Jason.
-
AuthorPosts
-
-
June 4, 2018 at 10:57 am #9035
SaraParticipantHello the community π
In my previous post
I thought I understood how relationships work.
But here I am stuck again.I’m still having my 2 CPT: city and event.
Each event owning its city.I initialized a custom rest api.
When a user searches for a city, I’d like to display its associated events.http://localhost/piklist/wp-json/apitest/v1/search?term=ibizaI also tried using new WP_query() instead of get_posts and dislaying with a while loop.
If I use'post_has' => $post->IDinstead of ‘relate_query’, all the events are displayed.
If I use'relate_query'I get nothing.Here’s where I stopped…
$events_query = get_posts(array( 'post_type' => 'event', 'numberposts' => -1, 'suppress_filters' => false, 'relate_query' => array( array( 'id' => $post->ID, 'relate'=> 'has' ) ) )); foreach ( $events_query as $event ) { array_push( $results['events'], array( 'title' => $event->post_title, 'link' => get_permalink($event->ID) )); }Could someone please help me on this?
I really wish I could understand better π
Thank you -
June 4, 2018 at 4:37 pm #9041
JasonKeymasterHello again, @Sara! π
Hmm… that’s interesting. I work with the REST API quite a bit on my agency’s projects and use relate queries β in fact, I’m developing a React + REST component right now.
Looking back on our previous thread, I think the issue is that event post type belongs to the city post type. So your
'relate' => 'has'should be'relate' => 'belongs', or use the'post_belongs' => $post->IDshorthand. Early in our last discussion I see I made this distinction, but then at the end I used thepost_hasshorthand for an even query, so that may be the source of the confusion. If that’s the case, I apologize for flipping that on you!Hope this helps! π
-
June 5, 2018 at 5:51 am #9042
SaraParticipantHi Jason,
Thank you for replying π
I got a little React training some months ago, I’m looking forward to getting back to it, I really loved it π
But here, I had tried
'relate' => 'belongs'already, but with same results.
If I use'relate_query'nothing happens, and if I use'post_belongs' => $post->IDall 4 events are pushed in the array :\Scratching my head…
-
June 5, 2018 at 1:49 pm #9043
JasonKeymasterBummer, I was hoping it was as simple as that.
So, to wrap my head around this, it looks like you’re creating a custom REST API endpoint, correct? Are you extending any of the existing endpoints (i.e. WP_REST_Posts_Controller)?
Does that exact query work outside of the REST API? Have you checked the output of the function via XDebug or something to see if the query is working but something else is the issue?
-
June 7, 2018 at 5:42 am #9044
SaraParticipantHello Jason π
Thank you for the XDebug advice, I didn’t know about it.
I’m currently trying to get it work on Atom…So… well, yes I’m creating a custom rest api endpoint.
And no I don’t extend anything with WP_REST_Posts_Controller (I wasn’t even aware of it by now).
I’ve registered a custom rest route, added my action and created my function which works as expected for other out of Piklist queries. So far, I’m using Postman to test my api queries results.And yes, that exact query works well outside of the REST API, with both
'relate_query'or'post_has'and aforeachfor the front end. Each city page displays the correct associated events.I’ll come back to let you know after I’ll test it out with XDebug.
If you ever come with some idea about this, don’t hesitate to drop me a line.Thank you very much π
-
June 15, 2018 at 5:42 am #9053
SaraParticipantHi Jason,
I’m back to tell how I succeeded making work my query into my custom api π
I was totally blind on this: I just had to make my query relate with the ID of the results in my json.
*facepalming myself*$events_query = new WP_Query(array( 'suppress_filters' => false, 'post_type' => 'event', 'numberposts' => -1, 'post_status' => 'publish', 'relate_query' => array( array( 'id' => $results['cities'][0]['id'], 'relate' => 'has' ) ) ));Thank you for your help π
-
June 15, 2018 at 1:21 pm #9054
JasonKeymasterHi Sara!
Glad you figured it out! I felt like it had to be something simple, as you were doing everything correctly as far as I could tell.
Happy coding! π
-
-
AuthorPosts
- You must be logged in to reply to this topic.