admin管理员组文章数量:1417577
I was creating a function which creates a new custom post type with the category or taxonomy name if it was created from the post editor. I am simply using the wp_insert_post function with the taxonomy name as the post title and content and I am hooking the function to the save_post hook.
The function is working fine on the classic editor. However, it is not working on the new Wordpress block editor. Once I save the post, it gives me "Updating failed" message. On the console, it gives me internal server error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error) with the REST API url as follows: http://localhost:8000/mywebsite/wp-json/wp/v2/posts/3447?_locale=user
I have checked the function and I made sure that the problem comes from the wp_insert_post function as updating the post works once I comment it out.
Here is the function:
function add_new_term_from_post($post_id) {
$settings_options = get_option('settings_options');
if($settings_options['redirect-directly'] === "1") {
$post_taxonomies = get_post_taxonomies($post_id);
wp_defer_term_counting(true);
foreach ($post_taxonomies as $post_taxonomy) {
if (in_array($post_taxonomy, $settings_options['show_taxonomies'])) {
$post_taxonomy_terms = wp_get_post_terms($post_id, $post_taxonomy);
foreach ($post_taxonomy_terms as $post_taxonomy_term) {
if($post_taxonomy_term->count === 1 ) {
if($post_taxonomy_term->name !== NULL && $post_taxonomy_term->name !== NULL && $post_taxonomy_term->name !== NULL) {
$term_post = array(
'post_title' => $post_taxonomy_term->name,
'post_content' => $post_taxonomy_term->name,
'post_excerpt' => $post_taxonomy_term->name,
'post_status' => 'publish',
'post_type' => 'whatever'
);
if(post_exists($post_taxonomy_term->name, "", "", "whatever") === 0) {
wp_insert_post($term_post);
}
}
}
}
}
}
wp_defer_term_counting(false);
}
}
add_action('save_post', 'add_new_term_from_post');
If someone knows why this happens and how it could be overcome, I would be grateful. Thanks
本文标签: block editorUpdating failed message when a wpinsertpost function is hooked to savepost hook
版权声明:本文标题:block editor - Updating failed message when a wp_insert_post function is hooked to save_post hook 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272558a2650982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论