【问题标题】:automatically create new post for each custom taxonomy term inserted by user为用户插入的每个自定义分类术语自动创建新帖子
【发布时间】:2018-07-03 21:24:39
【问题描述】:

好的,这就是我想要实现的目标。我有一个名为“Campaigns”的自定义帖子类型和一个名为“Countries”的自定义分类,已分配给该广告系列帖子类型。当用户添加新国家(将新国家标记到活动中)时,我想复制当前帖子,将新帖子命名为刚刚分配的国家/地区名称,并将其设为当前帖子的子项。

目前,我正在使用“set_object_terms”并保存条款以确定已创建哪些新条款。然后我尝试为每个术语插入一个新帖子,但是我正在创建一个无限循环。我尝试创建一个全局来停止循环,但我遇到了一些问题。这就是我到目前为止所拥有的。

$GLOBALS['control_campaign_count'] = 0;

add_action('set_object_terms','save_terms',10,6);
function save_terms($object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids) {

//Assuming we only want terms from specific taxonomy
//If not simply remove this conditional
$our_taxonomy = 'country';
if($taxonomy == $our_taxonomy){

    //Added terms are specified terms, that did not already exist
    $added_tt_ids = array_diff($tt_ids, $old_tt_ids);

    if ($append) {
        //If appending terms - nothing was removed
        $removed_tt_ids = array();
    } else {
        //Removed terms will be old terms, that were not specified in $tt_ids
        $removed_tt_ids = array_diff($old_tt_ids, $tt_ids);
    }

    /*, 
      Currently we have the taxonomy term IDs */


    foreach($added_tt_ids as $term){
        $added_terms[] = get_term_by( 'term_taxonomy_id',$term,$taxonomy);

        $name = $added_terms->name;
        echo $name;

        // Add New Post 
        add_action('wp_insert_post', 'add_new_campaign');
        function add_new_campaign() {

            if($GLOBALS['control_campaign_count'] === 1) {
                return;
            }

            $GLOBALS['control_campaign_count']++;

            $new_post_arguments = array(
                'post_title' => 'Japan',
                'post_content' => '',
                'post_status' => 'publish',
                'post_type'   => 'campaigns',
                'post_parent' => 1156
            );

            $id = wp_insert_post($new_post_arguments);
            update_post_meta($id, 'keywords');
        }
    }


    foreach($removed_tt_ids as $term){
        $removed_terms[] = get_term_by( 'term_taxonomy_id',$term,$taxonomy);


    }

    //$added_terms contains added term objects
    //$removed_terms contains removed term objects
}
}

想法?对实现这一目标的更好方法的想法?提前谢谢你。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我会使用相同的钩子。但是..

    这是我使用的代码,对我来说效果很好:

    add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
    function auto_create_campaigns( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
        if ( 'campaigns' !== get_post_type( $object_id ) || 'country' !== $taxonomy ) {
            return;
        }
    
        if ( $append ) {
            $term_ids = $tt_ids;
        } else {
            $term_ids = array_diff( $tt_ids, $old_tt_ids );
        }
    
        $post = get_post( $object_id, ARRAY_A );
        unset( $post['ID'] );
    
        foreach ( $term_ids as $term_id ) {
            // Remove the hook (to avoid infinite loop).
            remove_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
    
            $term = get_term( $term_id, $taxonomy );
            if ( is_wp_error( $term ) || empty( $term ) ) {
                continue;
            }
    
            $post['post_title'] = $term->name;
            $post['post_name'] = $term->slug;
            $post['post_parent'] = $object_id;
    
            $post_id = wp_insert_post( $post );
    
            /*if ( $post_id ) {
                // Copy the parent's "countries".
                // wp_set_post_terms( $post_id, $terms, $taxonomy );
            }*/
    
            /*if ( $post_id ) {
                // Copy the parent's custom fields.
                // ..
            }*/
    
            // Add it back.
            add_action( 'set_object_terms', 'auto_create_campaigns', 10, 6 );
        }
    }
    

    该代码只复制标准的帖子数据,例如post_title;但是,也可以复制父帖子的“国家/地区”/术语和/或自定义字段。

    更新

    移除钩子并将其添加回来(在foreach 循环中每次迭代的结束)可能比使用全局$_in_auto_create_campaigns 更好。

    希望这对您的孙子帖子也有帮助:

    如何设置条款,允许孙子发帖,而不是进入 无限循环?

    (您也可以尝试this,它使用相同的方法,但remove_action()add_action()放在wp_set_post_terms()附近

    【讨论】:

    • 这很棒,也适合我。感谢您的详细解答。
    • 我已经尝试为创建的帖子设置术语标签并且它有效,但是,随着条件检查 $_in_auto_create_campaigns 被触发,我只能深入一层。我正在寻找一个主帖子,例如市场营销>国家的子帖子(这是市场营销中的一个术语)>业务的孙子帖子(这是用户可以在该国家/地区设置的一个术语)。如何设置条款,允许孙子发帖,而不是陷入无限循环?
    猜你喜欢
    • 2021-01-29
    • 2013-06-21
    • 2018-01-08
    • 2016-07-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多