Как обновить количество записей в рубриках после импорта на сайт?

Как обновить количество записей в рубриках после импорта на сайт?

При импорте записей на сайт через XML у рубрик все по 0

Это нормальное поведение WordPress – счетчики таксономий (рубрик, меток) кэшируются и не всегда пересчитываются автоматически при импорте. Нужно принудительно обновить счетчики.

Скрипт для пересчета счетчиковСоздайте файл recalculate-counts.php в корневой папке сайта:

<?php
require_once('wp-load.php');

echo '<h2>Пересчет счетчиков рубрик и меток</h2>';

// Получаем все рубрики
$categories = get_terms(array(
    'taxonomy' => 'category',
    'hide_empty' => false,
));

echo "Найдено рубрик: " . count($categories) . "<br><br>";

foreach ($categories as $term) {
    // Принудительно пересчитываем счетчик
    wp_update_term_count($term->term_id, 'category', true);
    
    // Получаем обновленное количество
    $count = wp_count_terms(array(
        'taxonomy' => 'category',
        'parent' => 0,
        'term_id' => $term->term_id
    ));
    
    // Альтернативный способ подсчета
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'terms' => $term->term_id,
            ),
        ),
        'fields' => 'ids',
    );
    $real_count = count(get_posts($args));
    
    echo "Рубрика: {$term->name} (ID: {$term->term_id}) - Записей: $real_count<br>";
}

// То же самое для меток
$tags = get_terms(array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
));

echo "<br><h3>Метки</h3>";
echo "Найдено меток: " . count($tags) . "<br><br>";

foreach ($tags as $term) {
    wp_update_term_count($term->term_id, 'post_tag', true);
    
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'terms' => $term->term_id,
            ),
        ),
        'fields' => 'ids',
    );
    $real_count = count(get_posts($args));
    
    echo "Метка: {$term->name} (ID: {$term->term_id}) - Записей: $real_count<br>";
}

echo "<br><strong>Готово! Обновите страницу админки.</strong>";
?>

guest
Закрыть меню
ZAPLATA.RU | Магазин готовых блогов Wordpress с автонаполнением
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.