簡體   English   中英

自動為用戶插入的每個自定義分類術語創建新帖子

[英]automatically create new post for each custom taxonomy term inserted by user

好吧,這就是我要實現的目標。 我有一個名為“廣告系列”的自定義帖子類型和一個已分配給廣告系列帖子類型的自定義分類法“國家”。 當用戶添加新國家(向廣告系列添加新國家標簽)時,我要復制當前帖子,將新帖子命名為剛分配的國家/地區名稱,並將其作為當前帖子的子代。

目前,我正在使用“ 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
}
}

有想法嗎? 關於實現此目標的更好方法的想法? 先感謝您。

我會使用相同的鈎子。 但..

這是我使用的代碼,對我來說效果很好:

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數據,例如post_title 但是,也可以復制父帖子的“國家/地區” /術語和/或自定義字段。

更新

與使用全局$_in_auto_create_campaigns相比,最好刪除鈎子並將其添加回去 (在foreach循環中每次迭代的末尾 )。

希望這對您的孫子職位也有幫助:

如何設置條件,允許孫子發帖並且不陷入無限循環?

(您也可以嘗試使用相同的方法來嘗試this ,但是remove_action()add_action()放在wp_set_post_terms()附近

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM