функция
since 2.1.0
wp_set_post_categories()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_set_post_categories( int $post_id, int[]|int $post_categories = array(), bool $append = false ): array|false|WP_Error
Описание
Если категории не указаны, используется категория по умолчанию.
Оригинал (английский)
If no categories are provided, the default category is used.
Параметры
$post_id
int
необязательный
ID записи. Не принимает по умолчанию ID глобального объекта $post. Значение по умолчанию 0.
$post_categories
int[]|int
необязательный
= array()
Список ID категорий или ID одной категории.
$append
bool
необязательный
= false
Если true, существующие категории не удаляются, а только добавляются новые.
Если false, категории заменяются новыми.
Возвращаемое значение
array|false|WP_Error
WP_Error
Исходный код
wp-includes/post.php:5757
function wp_set_post_categories( $post_id = 0, $post_categories = array(), $append = false ) {
$post_id = (int) $post_id;
$post_type = get_post_type( $post_id );
$post_status = get_post_status( $post_id );
// If $post_categories isn't already an array, make it one.
$post_categories = (array) $post_categories;
if ( empty( $post_categories ) ) {
/**
* Filters post types (in addition to 'post') that require a default category.
*
* @since 5.5.0
*
* @param string[] $post_types An array of post type names. Default empty array.
*/
$default_category_post_types = apply_filters( 'default_category_post_types', array() );
// Regular posts always require a default category.
$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
if ( in_array( $post_type, $default_category_post_types, true )
&& is_object_in_taxonomy( $post_type, 'category' )
&& 'auto-draft' !== $post_status
) {
$post_categories = array( get_option( 'default_category' ) );
$append = false;
} else {
$post_categories = array();
}
} elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) {
return true;
}
return wp_set_post_terms( $post_id, $post_categories, 'category', $append );
}
История изменений
| Версия | Описание |
|---|---|
| 2.1.0 | Introduced. |

