add update rubrics
This commit is contained in:
@@ -960,3 +960,99 @@ function use_custom_template_for_en_subcategories($template) {
|
||||
|
||||
add_filter('template_include', 'use_custom_template_for_en_subcategories');
|
||||
|
||||
|
||||
/**
|
||||
* update rubrics
|
||||
*/
|
||||
|
||||
function add_posts_to_category_744() {
|
||||
// Получаем ID рубрики 740 и всех ее подрубрик
|
||||
$parent_category_id = 740;
|
||||
$subcategories = get_categories(array(
|
||||
'child_of' => $parent_category_id,
|
||||
'hide_empty' => false
|
||||
));
|
||||
|
||||
// Массив ID всех подрубрик 740
|
||||
$subcategory_ids = wp_list_pluck($subcategories, 'term_id');
|
||||
$all_category_ids = array_merge(array($parent_category_id), $subcategory_ids);
|
||||
|
||||
// Запрос для получения постов, которые имеют рубрику 740 или любую из ее подрубрик
|
||||
$args = array(
|
||||
'post_type' => 'post',
|
||||
'posts_per_page' => -1,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'category',
|
||||
'field' => 'term_id',
|
||||
'terms' => $all_category_ids,
|
||||
'operator' => 'IN'
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Получаем все посты с указанной рубрикой или ее подрубриками
|
||||
$query = new WP_Query($args);
|
||||
|
||||
if ($query->have_posts()) {
|
||||
while ($query->have_posts()) {
|
||||
$query->the_post();
|
||||
|
||||
// Получаем ID текущего поста
|
||||
$post_id = get_the_ID();
|
||||
|
||||
// Получаем все рубрики текущего поста
|
||||
$post_categories = wp_get_post_categories($post_id);
|
||||
|
||||
// Проверяем, есть ли у поста только рубрика 740
|
||||
if (count($post_categories) == 1 && in_array($parent_category_id, $post_categories)) {
|
||||
// Добавляем пост в рубрику с ID 744
|
||||
wp_set_post_categories($post_id, array_merge($post_categories, array(744)));
|
||||
} else {
|
||||
// Проверяем, если у поста есть подрубрика 740, но нет основной рубрики 740
|
||||
$has_subcategory = array_intersect($post_categories, $subcategory_ids);
|
||||
if ($has_subcategory && !in_array($parent_category_id, $post_categories)) {
|
||||
// Добавляем основную рубрику 740
|
||||
wp_set_post_categories($post_id, array_merge($post_categories, array($parent_category_id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
// Сбрасываем пост данные
|
||||
wp_reset_postdata();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function add_custom_admin_menu() {
|
||||
add_menu_page(
|
||||
'Update Posts Categories', // Заголовок страницы
|
||||
'Update Categories EN', // Текст меню
|
||||
'manage_options', // Возможность доступа (только администраторы)
|
||||
'update-categories', // Уникальный идентификатор страницы
|
||||
'update_categories_callback' // Функция для отображения контента страницы
|
||||
);
|
||||
}
|
||||
|
||||
add_action('admin_menu', 'add_custom_admin_menu');
|
||||
|
||||
|
||||
function update_categories_callback() {
|
||||
if (isset($_POST['update_categories'])) {
|
||||
add_posts_to_category_744();
|
||||
echo '<div class="updated"><p>Posts have been updated successfully.</p></div>';
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Update Posts Categories</h1>
|
||||
<form method="post">
|
||||
<input type="hidden" name="update_categories" value="1">
|
||||
<p>
|
||||
<input type="submit" class="button-primary" value="Update Categories">
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user