2016-11-25 41 views
0

我有問題申請誰創造了他們後用戶post_type,理論上我發現的WordPress的那些功能,但顯然這是行不通的不工作is_author(get_current_user_id())函數WordPress的

function preview_publish_posts($query) { 
    if(is_user_logged_in() && is_author(get_current_user_id())) { 
     $query->set('post_status', array('publish', 'pending')); 
    } 
    return $query; 
} 

add_filter('pre_get_posts', 'preview_publish_posts'); 

還是有可能的功能不適用於從functions.php的

+0

如果我沒有弄錯,is_author()沒有參數 - >當任何作者頁面正在顯示。帶參數的is_author()只會檢查是否顯示了作者檔案頁面,這是你想要做什麼? – Benoti

+0

我有一個非常類似的問題給你。當我刪除get_current_user_id()時,它完全適用於所有作者頁面,但我只希望它能夠在當前登錄的作者頁面上工作。此外,我不認爲你需要使用「返回$查詢」這個工作。 – mikey242

回答

0

稱這是很多玩圍繞後我發現,無論我怎樣努力is_author(get_current_user_id())將永遠不會返回true。我檢查了這是否是get_current_user_ID的錯誤,但是這會返回正確的值。我最終使用了一種稍微不同的方法來達到預期的結果。

下面的代碼解決了我的問題,但我仍然想知道爲什麼is_author(get_current_user_id())在這種情況下不起作用。

function preview_publish_posts($query) { 

    $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); 

    if (!is_admin() && $query->is_main_query() && is_author()){ 
     if(is_user_logged_in() && $curauth->ID == get_current_user_id()) {   
      $query->set('post_status', array('publish', 'pending')); 
     } 
     return; 
    } 
} 

add_action('pre_get_posts', 'preview_publish_posts');