16ответ(ов) в теме
хотя в масиве ,она поменялась
add_action( 'save_post', 'set_sandbox' );
function set_sandbox( $post_id ){
$category = get_the_category($post_id);
$category_item = $category[0]->cat_ID;
// Удаляем хук, чтобы не было зацикливания
remove_action( 'save_post', 'set_sandbox' );
// сохранием в мето поле
add_metadata( 'post', $post_id, 'post_category_old', $category_item, true );
// обновляем запись. В это время срабатывает событие save_post
print_r($category);
wp_set_post_categories( $post_id, 27, false );
$category2 = get_the_category($post_id);
print_r($category2);
// Ставим хук обратно
add_action( 'save_post', 'set_sandbox' );
}
Array
(
[0] => WP_Term Object
(
[term_id] => 1
[name] => Без рубрики
[slug] => bez_rubriki
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 34
[filter] => raw
[cat_ID] => 1
[category_count] => 34
[category_description] =>
[cat_name] => Без рубрики
[category_nicename] => bez_rubriki
[category_parent] => 0
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 27
[name] => Песочница
[slug] => sandbox
[term_group] => 0
[term_taxonomy_id] => 27
[taxonomy] => category
[description] =>
[parent] => 11
[count] => 24
[filter] => raw
[cat_ID] => 27
[category_count] => 24
[category_description] =>
[cat_name] => Песочница
[category_nicename] => sandbox
[category_parent] => 11
)
)
Array
(
)
Array
(
[0] => WP_Term Object
(
[term_id] => 27
[name] => Песочница
[slug] => sandbox
[term_group] => 0
[term_taxonomy_id] => 27
[taxonomy] => category
[description] =>
[parent] => 11
[count] => 24
[filter] => raw
[cat_ID] => 27
[category_count] => 24
[category_description] =>
[cat_name] => Песочница
[category_nicename] => sandbox
[category_parent] => 11
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 27
[name] => Песочница
[slug] => sandbox
[term_group] => 0
[term_taxonomy_id] => 27
[taxonomy] => category
[description] =>
[parent] => 11
[count] => 24
[filter] => raw
[cat_ID] => 27
[category_count] => 24
[category_description] =>
[cat_name] => Песочница
[category_nicename] => sandbox
[category_parent] => 11
)
)
Array
(
[0] => WP_Term Object
(
[term_id] => 27
[name] => Песочница
[slug] => sandbox
[term_group] => 0
[term_taxonomy_id] => 27
[taxonomy] => category
[description] =>
[parent] => 11
[count] => 24
[filter] => raw
[cat_ID] => 27
[category_count] => 24
[category_description] =>
[cat_name] => Песочница
[category_nicename] => sandbox
[category_parent] => 11
)
)
Редакции сообщения
25.09.2020 12:47Причина: не указано
Так правильно понял?
add_action( 'update_post_rcl', 'rcl_send_mail_about_new_post', 99, 1 );
function set_sandbox( $post_id ){
$category = get_the_category($post_id);
$category_item = $category[0]->cat_ID;
add_metadata( 'post', $post_id, 'post_category_old', $category_item, true );
// обновляем запись. В это время срабатывает событие save_post
}
вот так получилось ,спасибо
add_action( 'update_post_rcl', 'set_sandbox', 99, 1 );
function set_sandbox( $post_id ){
$category = get_the_category($post_id);
$category_item = $category[0]->cat_ID;
// сохраняем старуюю категорию
add_metadata( 'post', $post_id, 'post_category_old', $category_item, true );
// обновляем категорию
wp_set_post_categories( $post_id, 27, false );
}
Тема закрыта. Публикация новых сообщений запрещена.