[Resolved] Rest Api v2 does not work with my custom post type
This support ticket is created 5 years, 5 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
It's 404. Something strange is going on, because not even posts are working correctly. Can you try deactivating all plugins except Types and activating a default theme like Twenty Nineteen? Then test posts again. If the post appears in the REST API, reactivate your theme and other plugins one by one until the problem returns. Let me know what you find out.
Strange! The post 1202 worked e few hours ago.
I tried what you told me (deactivating all plugins except Types and activating Twenty Nineteen) but without any change.
What have I done before?
I made changes in the functions.php within the child theme and I activated the checkbox within the toolset post types settings "siegel".
What else could be the reason?
I'm not sure what could be happening. Can you try these troubleshooting steps?
- Temporarily switch to a default theme like Twenty Nineteen and deactivate ALL plugins.
- Test the post API endpoint: hidden link
- If the problem is resolved, reactivate your parent theme and test, then activate your child theme and test, and then activate Types, then each other plugin one by one, testing each time, until the problem returns.
- If the problem is not resolved, you may need to check your WordPress installation for errors and reinstall the latest version of WP. Otherwise, you may need to get your host involved.
Looks like it's working now: hidden link hidden link
Notice it's 'posts', not 'post'. I didn't see the problem before.
You can check all the routes available here: hidden link
Copy all that code and paste it into a JSON editor like https://jsoneditoronline.org to see the data model.
The API works indeed. A normal post is given out perfectly.
But unfortunately the content of the post (post-type "siegel") is not shown, because the content of "Siegel UHC Medien S" is loaded from the content template "template for seal S" and does not exist directly as the content of the entry.
Now the question is:
How do I also publish these contents of this page which are embedded via the content template?
The rendered template isn't included in the standard post response. I guess you could create your own custom endpoint that uses the PHP API to render a template and return that rendered HTML, or you could use rest_register_field to add a rendered template into the existing post response. So it depends on how you want to access that data.
The aaa.php is an external page simply for testing the Headless output of hidden link.
I still do not understand how to output the content generated using the template (hidden link) in the post-type "siegel". It seems, the content generated by the toolset template does not appear here.
hidden link
What do I have to change to output the content generated by the toolset template?
I still do not understand how to output the content generated using the template in the post-type "siegel". It seems, the content generated by the toolset template does not appear here.
Correct, the rendered template does not exist in the post object. You may add the rendered Content Template to the data model, similar to how you used register_rest_field to add post-meta-fields. Use register_rest_field to add another field to the data model. This field will hold the rendered Content Template. You can render a Content Template for any post using the PHP API render_view_template. Here is the general idea:
// register a new field to hold the rendered template
register_rest_field( 'siegel', 'post-rendered-template', array(
'get_callback' => 'get_post_rendered_template_for_api',
'schema' => array(
'description' => __( 'Rendered Siegel Content Template' ),
'type' => 'string'
),
)
);
// render the content template for this siegel post using render_view_template
// then add it to the data model
function get_post_rendered_template_for_api( $object ) {
global $current_user;
$content_template_id = 12345;
$the_post = get_post($object['id']);
$output = render_view_template( $content_template_id, $the_post, $current_user );
return $output;
}
Then the rendered template code can be found in the data model as post-rendered-template