1

I'm trying to find a way to show different content if a post is set to "password protected", once the user entered the correct password.

I want to show the price of an art piece only on posts that are set to password protected.

I can't find any php code to achieve this, since all I can find includes cookies management.

something like :

<?php if($post is set to password protected): ?>

show price

<?php endif; ?>

is there a way to do this ? there must some data stored into the database somewhere right ?

any help would be greatly appreciated ;)

2 Answers 2

1

There's two main ways:

  1. You can use the post_password_required() function. This function returns true if the post has a password and false if the post doesn't have a password, but it also returns false if the post had a password but the user has entered it and unlocked the post.
  2. You can get the post object with $post = get_post() and then check empty( $post->post_password ). This will tell you whether a post has a password regardless of whether or not the password has been entered.
0

For those prefer use SQL...

SELECT * FROM `wp_posts` WHERE `post_password` != ''

Note: mind to check about empty chars (like a empty space) in post_password field, it might confuse you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.