【发布时间】:2022-11-23 03:11:35
【问题描述】:
我的自定义帖子类型“references”有一个名为“references_count”的自定义字段。它有一个数值。
我有一个名为“country”的自定义分类法,其中包含一个名为“country_count”的自定义字段。
背景:自定义帖子类型“参考”保存城市与一些这个城市的客户.该值保存在字段“references_count”中。在海关分类法中有国家。每个国家都有一个参考文献总数.
例子:在“柏林”市,有 3 个客户。在“慕尼黑”市有 2 个客户。分类术语“德国”包括该国家/地区所有城市的总和。因此,此示例中分类术语“德国”的“country_count”的值为 5,是每个城市的引用之和。
如果我要保存每个单独的分类术语,我编写的代码可以正常工作。
add_action( 'edited_country', 'update_counter_for_countries', 10, 2 );
function update_counter_for_countries( $term_id ) {
// Get posts with term
$args = array(
'post_type' => 'reference',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'country',
'field' => 'term_id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
// sum values in posts
$sumTerm = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$number = get_field( 'references_count', get_the_ID() );
$sumTerm = $sumTerm + $number;
}
}
wp_reset_postdata();
// update field in term
update_field( 'country_count', $sumTerm, 'country'.'_'.$term_id );
}
问题:我有 100 多个国家(分类术语),所以我必须单独保存每个术语才能让事情顺利进行。
我在找什么:有没有办法一次更新/保存所有自定义分类术语,这样我就不必分别更新每个术语?我检查了很多插件,但找不到任何插件可以提供“批量编辑”或“批量保存”分类术语。如果可能的话,我更喜欢没有插件的解决方案。我非常感谢任何提示,非常感谢。
【问题讨论】:
标签: wordpress custom-post-type custom-taxonomy bulk taxonomy-terms