Skip Navigation

[Resolved] Can’t expose relationship intermediary posts to WP API

This thread is resolved. Here is a description of the problem and solution.

Problem:
The user would like to publish intermediary posts on the WordPress REST API.

Solution:
This needs custom code to make the intermediary post type public in the REST API. This is an example:

add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
  
function my_post_type_args( $args, $post_type ) {
  
    if ( 'couples' === $post_type ) {
        $args['show_in_rest'] = true;
  
        // Optionally customize the rest_base or rest_controller_class
        $args['rest_base']             = 'couples';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
  
    return $args;
}

More about it on this reply https://toolset.com/forums/topic/cant-expose-relationship-intermediary-posts-to-wp-api/#post-2161249

This support ticket is created 3 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 3 replies, has 2 voices.

Last updated by parisK 3 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#2160573
screenshot_1.png

Tell us what you are trying to do?
Hi, I have a simple case. I have already created two CPTs the "candidate-profile" and the "job-openings". Now I want to create a many-to-many relationship between these two CPTs meaning the "multiple candidate fits with multiple job openings" so I created a relationship called "couples" (see screenshot_1.png ). All these have been done successfully, I can select multiple job openings for each candidate profile.

What I need to do now, is to access the couples CPTs through the WP API. Since I have access to every post type through the WP API I thought that I should visit "/wp-json/wp/v2/couples" since the name of my relationship intermediary posts is couples. However, this returns "{"code":"rest_no_route","message":"No route was found matching the URL and request method.","data":{"status":404}}". In simple terms, I need to GET and POST "couple" posts through the WP API.

* right now the WP API is closed only for Admins, so you can request admin access through a safe form.

Thank you in advance

#2160681

Hello and thank you for contacting Toolset support.

Honestly, I am not sure if the intermediary post type is meant to be public to the site users. Especially if it is not visible in the admin area. I need to confirm that with our 2nd Tier.

In the meantime, I'd like to ask for the following:
- When creating the relationship, have you configured the intermediary post type to be visible in the admin? Does it appear in the admin?
- I'd like to access your website and check the current setup closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2161249

Thank you for the credentials. I was able to expose the intermediary post using custom code. You can check this URL hidden link

First, I went through our documentation and all the available REST integration is only for custom fields. Check this article https://toolset.com/documentation/programmer-reference/toolset-integration-with-the-rest-api/

Toolset does not offer a built-in way to publish relationships through REST API. You will need to extend the WordPress API and query the data using the relationship API.
- https://developer.wordpress.org/rest-api/
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Because the intermediary posts are a separate custom post type, we can hook at its registration arguments and make it available in the REST API. The following code works for your case:

add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
 
function my_post_type_args( $args, $post_type ) {
 
    if ( 'couples' === $post_type ) {
        $args['show_in_rest'] = true;
 
        // Optionally customize the rest_base or rest_controller_class
        $args['rest_base']             = 'couples';
        $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
    }
 
    return $args;
}

I added it in the theme's functions.php file. Because this snippet will not work as expected inside a Toolset snippet, it has to be added to the theme or a separate plugin https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#snippet-execution-timing

Once you update the theme this custom code will be lost, please consider one of the following solutions:
- Use a child theme and add the snippet in the funtions.php file. Check this article for the Astra theme hidden link
- Create a separate plugin and add the snippet to it. Then activate the plugin. Check this article hidden link

I hope this helps. Let me know if you have any questions.

#2161739

My issue is resolved now. Thank you!
Wow, really appreciated, Thank you so much!