【问题标题】:Bulk update all taxonomy terms in wordpress批量更新 wordpress 中的所有分类术语
【发布时间】: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


    【解决方案1】:

    您可以使用此代码一次性更新所有条款。 只要确保备份您的数据库以备不时之需。

    此代码将遍历所有条款,并且只会运行一次。之后您可以删除此代码。

    为了使此代码仅在您的 IP 上运行,请将 111.111.111.111 更改为您的 IP 地址。

    if($_SERVER["REMOTE_ADDR"]=='111.111.111.111'){
    //run only my ip
        add_action("init","update_all_terms_in_one_go");
    }
    
    function update_all_terms_in_one_go(){
        session_start();
        if(isset($_SESSION['all_terms_updated']) && $_SESSION['all_terms_updated'] == "done"){
            return;
        }
        $taxonomy = "country";
        $terms = get_terms([
            'taxonomy' => $taxonomy,
            'hide_empty' => false,
        ]);
        foreach ($terms as $term) {
            update_counter_for_countries( $term->term_id );
        }
        $_SESSION['all_terms_updated'] = "done";
        echo "ALL TAXONOMY TERMS UPDATED";
        die();
    
    }
    
    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 );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多