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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745272559a4619837.html
评论列表(0条)