2014-08-31 19 views
0

我正在嘗試使用作者元數據自動填充給定作者的帖子上的自定義分類。目前,分類標準在帖子頁面上可見,但我打算從前端隱藏它,以避免意外的用戶選擇。 (也是預填充的原因)根據發佈作者元數據自動填充後期分類數據?

注意:相關分類術語和作者元數據是相同(作者元數據選項是從現有分類術語中動態提取的)。

由於某種原因 - 這是行不通的。我摸不着頭腦,試圖找出原因 - 我認爲邏輯是正確的。我錯過了什麼/任何人都可以幫忙嗎?

在此先感謝。

function update_school($post_id) { 

    $post_author_id = get_post_field('post_author', $post_id); // get the post author ID 
    $school_title = get_the_author_meta('school2', $post_author_id); // from the post author ID, get the author meta 
    $school_slug = get_the_author_meta('school', $post_author_id); // from the post author ID, get the author meta 

    $school_term = get_term_by('slug', $school_slug, 'school'); // get term by the author meta called "school" 
    $school_term_name = $school_term->name; // from the slug, get the school term name 


    // update the post taxonomy "school" with author meta school variables above 
    wp_set_post_terms($post_id, $school_term_name, 'school'); 

} 

// run function on post save 
add_action('save_post', 'update_school'); 

回答

0

想通了,誰想要知道:

function update_school($post_id) { 
    $post_author_id = get_post_field('post_author', $post_id); // get the post author ID 
    $school_name = get_the_author_meta('school2', $post_author_id); // from the post author ID, get the author meta 
    $term = term_exists($school_name, 'school'); 
    wp_set_post_terms($post_id, $term, 'school'); 

} 

// run function on post save 
add_action('save_post', 'update_school');