批量导入/删除后如何更新WordPress分类(类别/标签)'计数字段

kao*_*obo 13 wordpress

批量导入/删除后如何更新WordPress分类(类别/标签)'计数字段?

相关问题:

WordPress>支持»修复导入后的注释和类别计数 http://wordpress.org/support/topic/fix-comment-and-category-counts-after-import

kao*_*obo 61

这个SQL有助于:

UPDATE wp_term_taxonomy SET count = (
SELECT COUNT(*) FROM wp_term_relationships rel 
    LEFT JOIN wp_posts po ON (po.ID = rel.object_id) 
    WHERE 
        rel.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
        AND 
        wp_term_taxonomy.taxonomy NOT IN ('link_category')
        AND 
        po.post_status IN ('publish', 'future')
)
Run Code Online (Sandbox Code Playgroud)


Mar*_*com 16

更符合WordPress的方式wp_update_term_count_now(https://developer.wordpress.org/reference/functions/get_terms/)

例:

$update_taxonomy = 'my_taxonomy';
$get_terms_args = array(
        'taxonomy' => $update_taxonomy,
        'fields' => 'ids',
        'hide_empty' => false,
        );

$update_terms = get_terms($get_terms_args);
wp_update_term_count_now($update_terms, $update_taxonomy);
Run Code Online (Sandbox Code Playgroud)


Edd*_*e B 6

@kaorukobo 的回答效果很好。但是,我需要更多的自定义来自动更新 CPT 的所有术语……特别是具有多个自定义分类法的 WooCommerce 产品 CPT……

以下更新了产品 CPT 的所有分类法。

UPDATE wp_term_taxonomy tt
    SET count =
    (SELECT count(p.ID) FROM  wp_term_relationships tr
    LEFT JOIN wp_posts p
    ON (p.ID = tr.object_id AND p.post_type = 'product' AND p.post_status = 'publish')
    WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
)
Run Code Online (Sandbox Code Playgroud)


小智 6

在我看来,最好的方法是使用WP-CLI。有一个命令可以做到这一点:

$ wp term recount category

在此处查找文档:https://developer.wordpress.org/cli/commands/term/recount/