Viewing 6 reply threads
  • Author
    Posts
    • #9035
      Sara
      Participant

      Hello the community πŸ™‚

      In my previous post

      Select CPT and display it on another CPT

      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=ibiza

      I also tried using new WP_query() instead of get_posts and dislaying with a while loop.
      If I use 'post_has' => $post->ID instead 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

    • #9041
      Jason
      Keymaster

      Hello 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->ID shorthand. Early in our last discussion I see I made this distinction, but then at the end I used the post_has shorthand 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! πŸ™‚

    • #9042
      Sara
      Participant

      Hi 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->ID all 4 events are pushed in the array :\

      Scratching my head…

    • #9043
      Jason
      Keymaster

      Bummer, 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?

    • #9044
      Sara
      Participant

      Hello 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 a foreach for 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 πŸ™‚

    • #9053
      Sara
      Participant

      Hi 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 πŸ™‚

    • #9054
      Jason
      Keymaster

      Hi 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! πŸ™‚

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