2011-07-26 159 views
1

任何人都可以解釋我如何更改以下代碼以排除受密碼保護的帖子嗎?我知道我可以在while語句中使用if語句,但我想從WP_Query點中排除它們。從wordpress頁面排除受密碼保護的帖子

$pq = new WP_Query(array('post_type' => $ptype, 'showposts' => $pshow)); 

回答

2

你可以把它用post_where過濾器發生,您執行查詢之前:

function getNonPasswordedPosts(){  
    // Add the filter to exclude passworded posts 
    add_filter('posts_where', 'excludePassworded'); 

    // Query for the posts 
    $pq = new WP_Query(array('post_type' => $ptype, 'showposts' => $pshow)); 

    // Remove the filter so that other WP queries don't exclude passworded posts 
    remove_filter('posts_where', 'excludePassworded'); 

    // iterate over returned posts and do fancy stuff  
} 

function excludePassworded($where) { 
    $where .= " AND post_password = '' "; 
    return $where; 
} 
+0

使用remove_filter(「posts_where」你應該也可能刪除查詢過濾器後,「excludePassworded」 ); – HWD

+0

良好的致電@HWD - 我已經更新了我的答案。 – Pat

相關問題