批量导入/删除后如何更新WordPress分类(类别/标签)'计数字段?
相关问题:
WordPress>支持»导入后修复评论和类别计数 http://wordpress.org/support/topic/fix-comment-and-category-counts-after-import
答案 0 :(得分:57)
此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')
)
答案 1 :(得分:14)
更符合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);
答案 2 :(得分:4)
@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
)
答案 3 :(得分:1)
我认为最好的方法是使用WP-CLI。有一个命令:
HeightRequest
在此处找到文档:https://developer.wordpress.org/cli/commands/term/recount/