Пост не отображается в таксономии после добавления его с помощью wp_insert_post
Добавляю пост с помощью wp_insert_post, все хорошо, все добавляется. Также нужная таксономия проставляется. Но в данной таксономии пост не отображается и начинает отображаться только если в редактировании поста апдейтнуть его, даже ничего не меняя. Куда копать?
P.S. таксономия добавляется через wp_set_object_terms.
$post_data = [
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'other',
'post_author' => 1,
'post_category' => [ 1 ],
];
$post_id = wp_insert_post( $post_data, true );
wp_set_object_terms( $post_id, $term_value, 'news', true );
Ответы (1 шт):
Автор решения: KAGG Design
→ Ссылка
Проблема в каких-то плагинах. На чистом WP ваш код работает.
Отключите все плагины, поставьте стандартную тему, добавьте в mu-plugins такой файл и убедитесь, что всё работает.
<?php
/**
* Register Notification custom post type.
*/
function so_register_cpt_notification() {
$labels = [
'name' => 'Notifications',
'singular_name' => 'Notification',
'add_new' => 'Add New',
'add_new_item' => 'Add New Notification',
'edit_item' => 'Edit Notification',
'new_item' => 'New Notification',
'view_item' => 'View Notification',
'search_items' => 'Search Notifications',
'not_found' => 'Not Found',
'not_found_in_trash' => 'Not Found In Trash',
'parent_item' => 'Parent',
'parent_item_colon' => 'Parent:',
'menu_name' => 'Notifications',
];
$args = [
'labels' => $labels,
'hierarchical' => false,
'description' => 'Notifications',
'supports' => [
'title',
'editor',
],
'taxonomies' => [ 'channel' ],
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( '<svg height="20" viewBox="0 85.5 1024 855" version="1.1" xmlns="http://www.w3.org/2000/svg"><path d="M490.666667 938.666667c46.933333 0 85.333333-38.4 85.333333-85.333334h-170.666667c0 46.933333 38.4 85.333333 85.333334 85.333334z m277.333333-256V448c0-130.986667-90.88-240.64-213.333333-269.653333V149.333333c0-35.413333-28.586667-64-64-64s-64 28.586667-64 64v29.013334C304.213333 207.36 213.333333 317.013333 213.333333 448v234.666667l-85.333333 85.333333v42.666667h725.333333v-42.666667l-85.333333-85.333333z" fill="black" /> </svg>' ),
// phpcs:enable WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => [
'slug' => 'notification',
'with_front' => false,
],
'capability_type' => 'post',
];
register_post_type( 'notification', $args );
}
add_action( 'init', 'so_register_cpt_notification' );
/**
* Register taxonomies for Notification custom post type.
*/
function so_register_taxonomies() {
$args = [
'labels' => [
'name' => 'Channels',
'singular_name' => 'Channel',
'search_items' => 'Search Channels',
'all_items' => 'All Channels',
'parent_item' => 'Parent Channel',
'parent_item_colon' => 'Parent Channel:',
'edit_item' => 'Edit Channel',
'update_item' => 'Update Channel',
'add_new_item' => 'Add New Channel',
'new_item_name' => 'New Channel',
'menu_name' => 'Channels',
],
'description' => 'Notification Channels',
'public' => false,
'show_ui' => true,
'hierarchical' => false,
'meta_box_cb' => null,
'show_admin_column' => false,
];
register_taxonomy( 'channel', [ 'notification' ], $args );
}
add_action( 'init', 'so_register_taxonomies' );
/**
* Add post and set term for it.
*/
function so_term_cache() {
$post_data = [
'post_title' => 'Post term cache test',
'post_status' => 'publish',
'post_type' => 'notification',
'post_author' => 1,
];
$post_id = wp_insert_post( $post_data, true );
wp_set_object_terms( $post_id, 'foobar', 'channel', true );
}
add_action( 'init', 'so_term_cache', 20 );