Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude password protected posts in WordPress loop

Tags:

wordpress

I have a custom post type that supports password protected entries. In a custom loop using a new WP_Query object, I want to exclude those password protected posts from the results. What arguments do I need set in order to do this? I am using the latest trunk version of WordPress 3.2.1.

like image 569
Kevin Avatar asked Sep 24 '11 12:09

Kevin


1 Answers

I come up to this question where I was looking for the same. However, I read WP_Query document line by line then found very simple solution and that is just to add 'has_password' => false argument to the query $args

So the code will be as below...

$args  = [
    'post_type'      => [ 'post', 'page' ],
    'posts_per_page' => 3,
    'post__not_in'   => get_option( 'sticky_posts' ),
    'has_password'   => FALSE
];

Here you can see I have excluded Sticky and Password Protected posts.

like image 72
Code Lover Avatar answered Sep 17 '22 13:09

Code Lover