dttvb115151 2018-07-03 21:24
浏览 44
已采纳

自动为用户插入的每个自定义分类术语创建新帖子

Alright, here's what I'm trying to achieve. I have a custom post type called 'Campaigns' and a custom taxonomy called 'Countries' that has been assigned to the campaign post type. When a user adds a new country (tags a new country to a campaign) I want to duplicate the current post, name the new post the name of the country just assigned, and make it a child of the current post.

Currently, I'm hooking into 'set_object_terms' and on the save of the terms determining what new terms have been created. I'm then trying to insert a new post for each of the terms, however I'm creating an infinite loop. I've tried creating a global to stop the loop, but I'm running into some issues. This is what I have so far.

$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
}
}

Ideas? Thoughts on a better way to achieve this? Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongsong8932 2018-07-04 11:49
    关注

    I'd use the same hook. But..

    Here's the code I used, which worked well for me:

    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 );
        }
    }
    

    That code only copies the standard post data such as post_title; however, it is possible to also copy the parent post's "countries"/terms and/or custom fields.

    UPDATE

    It's probably better to remove the hook and add it back (at the end of each iteration in the foreach loop) than using the global $_in_auto_create_campaigns.

    Hopefully that also helps you with the grandchild posts:

    How can I set the terms, allow grandchild posts, and not get in an infinite loop?

    (You can also try this, which uses the same method, but the remove_action() and add_action() are placed near the wp_set_post_terms())

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 chatgpt网页版侧边栏和模型切换点击没反应怎么办
  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas