0

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?

2 Answers 2

2

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:

0

Try this

    global $wpdb;
    $user = wp_get_current_user();
    $sql = "SELECT id from " . $wpdb->prefix . "rg_lead WHERE created_by=" . $user->ID;
    $results = $wpdb->get_results($sql);
    if (count($results) > 0) {
        // An entry is made by the user for any form
    }
    else{ 
         // Show form
    }

If you know form id for example $form_id then use

$sql = "SELECT id from " . $wpdb->prefix . "rg_lead WHERE created_by=" . $user->ID . " AND form_id=" . $form_id;

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.