2017-01-01 7 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); 
関連する問題