2015-07-02 38 views
2

我有了這個腳本列出所有的文章標題用的永久鏈接,包括職位,是在垃圾桶裏,在前端:訪問丟棄在前端WordPress的帖子

<ul> 
<?php 
$myposts = get_posts(array(
    'numberposts' => -1, 
    'offset' => 0, 
    'category' => $cat_id, 
    'post_status' => array('publish','trash') 
    ) 
); 
foreach($myposts as $post) : 
setup_postdata($post); 
?> 

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 

<?php endforeach; ?> 
<?php wp_reset_postdata(); ?> 
</ul> 

這工作正常。但問題是,如果我點擊「垃圾箱」中的任何帖子標題,我會看到404頁面。

如何訪問前端的過期帖子?我明白這是默認的WordPress的行爲,但也許有一個功能,允許垃圾郵件被查看?

在此先感謝。

+0

我很好奇,爲什麼你想在前端顯示垃圾帖子,如果你不介意的話,謝謝。 – birgire

回答

4

默認情況下,主查詢僅顯示所有用戶和已登錄用戶的其他私人帖子的已發佈帖子。因此,我們可以使用pre_get_posts鉤子附加後的狀態添加到主查詢

這是沒有經過測試的,不知道它會工作,但你可以嘗試以下

add_action('pre_get_posts', function ($q) 
{ 
    if ( $q->is_main_query() 
     && $q->is_single() // can replace with $q->is_singular() 
    ) { 
     $q->set('post_status', array('publish','trash')); 
    } 
}); 
+0

謝謝@Pieter Goosen。它的錯誤: '分析錯誤:語法錯誤,意外'功能'(T_FUNCTION)' – user3256143

+0

對不起,有一個缺少逗號。固定。請參閱我的更新代碼 –

+0

哇!老實說,我不認爲這在前端是可能的。乾杯。賞金獎勵:-) 更新:當我試圖獎賞賞金時,它說「你可以在21小時內獎勵你的賞金」。我會在21小時內回來:-) – user3256143