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
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. **
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.
My issue is resolved now. Thank you!
Wow, really appreciated, Thank you so much!