Skip Navigation

[Gelöst] Rest Api v2 does not work with my custom post type

This support ticket is created vor 5 Jahren, 5 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 18 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 5 Jahren, 5 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1278829
toolset_cpt_siegel.jpg

I am trying to:
I am trying to get wordpress rest api v2 to work with my custom post type "siegel" created in types.

Link to a page where the issue can be seen:
versteckter Link

I expected to see:
The content of that page (Post-ID: 958):
versteckter Link

Instead, I got:
no Content of the CPT

It works perfect with the content of a post
e.g. versteckter Link

but not with my CPT "siegel"
versteckter Link

What I tried already:
I added the following function:

add_filter( 'wpcf_type', 'add_show_in_rest_func_siegel', 10, 2);
function add_show_in_rest_func_siegel($data_siegel, $post_type01) {
	if($post_type01 == 'siegel'){
		$data_siegel['show_in_rest'] = true;
		$data_siegel['rest_base'] = 'custom';
	}
	return $data_siegel;
}

Thanks for a helpful hint.
Regards,
Uwe

#1278873

Use an incognito browser and look here :

versteckter Link

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.

#1278987

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?

#1279005

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: versteckter 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.

#1281001

Hi Christian,

I tried everything an contacted the host - without succes.

Do you have any other idea?

Thanks and best regards
Uwe

#1281137

Looks like it's working now:
versteckter Link
versteckter Link

Notice it's 'posts', not 'post'. I didn't see the problem before.

You can check all the routes available here:
versteckter Link
Copy all that code and paste it into a JSON editor like https://jsoneditoronline.org to see the data model.

#1281519

Thank you very much Christian!
The "s" was the problem.
Great support!
Thanks and best regards

#1281951

Okay great, I'm marking this ticket as resolved.

#1282113
problem_json_siegel01.jpg
problem_json_siegel02.jpg
problem_json_siegel03.jpg

Hi Christian,

sorry, I´d like to reopen this ticket.

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?

Thanks for the renewed support.
Best regards
Uwe

#1282213

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 render_view_template method will allow you to apply any Content Template to any post:
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

#1282707

Thank you.
I tried to add this to the functions.php (child-theme):

add_action( 'rest_api_init', 'create_api_posts_meta_field');
function create_api_posts_meta_field() {

// register_rest_field ( 'name-of-post-type', 'name-of-field-to-return', array-of-callbacks-and-schema() )
register_rest_field( 'siegel', 'post-meta-fields', array(
'get_callback' => 'get_post_meta_for_api',
'schema' => null,
)
);
}

function get_post_meta_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];

//return the post meta
return get_post_meta( $post_id );
}

Unfortunately without positive result:
versteckter Link

#1283247

I don't really know what the aaa.php code does, but the post_meta_fields field seems to be added to the REST response successfully:
versteckter Link

Maybe there is a problem in aaa.php?

#1283827

The aaa.php is an external page simply for testing the Headless output of versteckter Link.

I still do not understand how to output the content generated using the template (versteckter Link) in the post-type "siegel". It seems, the content generated by the toolset template does not appear here.

versteckter Link

What do I have to change to output the content generated by the toolset template?

#1285007

Shane
Supporter

Sprachen: Englisch (English )

Zeitzone: America/Jamaica (GMT-05:00)

Hi Uwe,

Christian is currently out today but will return on Sunday to continue assisting you with this one.

Thanks,
Shane

#1285683

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