Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have gravity forms set up on my site and I am wanting to hide/show a form based on whether or not the user has posted an entry into the form/database. If they have made an entry, then I want to hide it and display some simple text.

I see that gravity forms allow you to limit number of entries but this is not quite what I am after.

I'm guessing I would have to query the database but I'm not really sure the best way to write it. Would I have to use something like get_posts and check if the author has an entry? Would this be the best way to do it?

share|improve this question

1 Answer 1

Try this;

<?php 

    $user = wp_get_current_user();

    if ( count_user_posts( $user->ID ) >= 1 ) {

        // echo your text snippet

    } else {

        // echo your form

    }

?>

1) wp_get_current_user() returns the WP_User object which includes many properties for that user.

2) Since we only want the ID of the current user, we assign a variable of choice, in this instance $user which is equal to that of i.e. $user = wp_get_current_user().

3) Then we can grab the current user ID simply by declaring $user->ID

4) We pass $user->ID to our count_user_posts( $user->ID) function which is then measured against our condition being greater than or equal to 1 post.

5) Do your thing...

References:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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