2017-01-01 50 views
0

我想保存後獲取帖子標籤列表。WordPress的:如何獲得新的帖子標籤

當後保存在我第一次得到一個空數組,但當保存第二次(編輯)標籤正確顯示,

這裏是我使用來獲取標籤的代碼:

add_action('save_post', 'collect_tags'); 
// ... 
function collect_tags($postId){ 
    $terms = get_object_term_cache($postId, 'post_tag'); 
    if (false === $terms) { 
     $terms = wp_get_object_terms($postId, 'post_tag'); 
    } 
    if(empty($terms)) { 
     $terms = wp_get_post_tags($postId); 
    } 
    return $terms; 
} 

請問有誰能指點我的錯誤在哪裏?

我使用WordPress版本4.7

+0

你知道你可以從後拔出標籤,沒有任何自定義保存功能? [get_the_tags](https://codex.wordpress.org/Function_Reference/get_the_tags),檢查相關部分是否有與標籤相關的其他功能... –

+0

@dingo_d感謝您的評論,實際上這是我的簡單版本插件爲了測試水域,我需要這個工作才能繼續插件的開發 –

回答

0

嘗試

function collect_tags($post_id, $post, $update) { 
    $terms = get_object_term_cache($post_id, 'post_tag'); 
    if (false === $terms) { 
     $terms = wp_get_object_terms($post_id, 'post_tag'); 
    } 
    if(empty($terms)) { 
     $terms = wp_get_post_tags($post_id); 
    } 

    // --- TEST --- 
    $tags = wp_get_post_tags($post_id); 
    print_r($tags); 
    // --- END --- 

    return $terms; 
} 
add_action('wp_insert_post', 'collect_tags', 10, 3); 
相關問題