From 641399598faa2df2d2b944ba11fd5515d7ada6a4 Mon Sep 17 00:00:00 2001 From: argoexpert press Date: Sat, 25 May 2024 16:51:55 +0300 Subject: [PATCH] add update rubrics --- category-en.php | 4 +- en/category-en-sub.php | 2 +- en/menu-primary-en.php | 10 +++++ functions.php | 96 ++++++++++++++++++++++++++++++++++++++++++ header-en.php | 3 +- header.php | 3 +- 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 en/menu-primary-en.php diff --git a/category-en.php b/category-en.php index 4b49055..094cd40 100644 --- a/category-en.php +++ b/category-en.php @@ -8,7 +8,7 @@ $args = array( 'post_type' => 'post', // Или ваш пользовательский тип постов, если необходимо 'post__not_in' => [ $mainpost_id ], - 'posts_per_page' => 10, // Количество постов на странице + 'posts_per_page' => 20, // Количество постов на странице 'orderby' => 'date', // сортировка по дате 'order' => 'DESC', // по убыванию (сначала новые) 'ignore_sticky_posts' => 1, @@ -18,7 +18,7 @@ ?> false, 'query' => $query]); ?> - + diff --git a/en/category-en-sub.php b/en/category-en-sub.php index 62c9d27..81d24bb 100644 --- a/en/category-en-sub.php +++ b/en/category-en-sub.php @@ -11,7 +11,7 @@ $paged = (get_query_var('paged')) ? absint(get_query_var('paged')) : 1; $args = array( 'post_type' => 'post', - 'posts_per_page' => 5, + 'posts_per_page' => 9, 'paged' => $paged, 'category__in' => get_queried_object_id() ); diff --git a/en/menu-primary-en.php b/en/menu-primary-en.php new file mode 100644 index 0000000..0db113e --- /dev/null +++ b/en/menu-primary-en.php @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/functions.php b/functions.php index b7d685a..389f464 100644 --- a/functions.php +++ b/functions.php @@ -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 '

Posts have been updated successfully.

'; + } + ?> +
+

Update Posts Categories

+
+ +

+ +

+
+
+ +
RU