Skip Navigation

[Resolved] Conditional display checking that no posts exist by current user

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am building a site which includes reviews for users. I am trying to display the CRED form for review submission on the profile page (another custom post type). However I only want to display the form to create a review if there are no existing reviews by the logged in user for the profile being displayed.

This is the function code I wrote:
https://toolset.com/forums/topic/conditional-display-checking-that-no-posts-exist-by-current-user/#post-683641

Solution:

It is a custom codes problem, please check this:

1) Output the shortcode [review_exists] directly, check if it is does output the correct number count of current user.

2) Edit your codes as below:
https://toolset.com/forums/topic/conditional-display-checking-that-no-posts-exist-by-current-user/#post-684133
And test again.

Relevant Documentation:

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.76 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#683641

Tell us what you are trying to do?

I am building a site which includes reviews for users. I am trying to display the CRED form for review submission on the profile page (another custom post type). However I only want to display the form to create a review if there are no existing reviews by the logged in user for the profile being displayed.

Is there any documentation that you are following?

I have been looking at this thread: https://toolset.com/forums/topic/conditional-display-of-cred-form-on-post-depending-if-a-child-post-exists/

This is the function code I wrote:


function check_review_exists( ) {
      
    $user = wp_get_current_user();
    $result = new WP_Query(array(
        'author'            => $user->ID,
        'post_type'     => 'supplier-review', 
        'post_status'       => 'publish',
        'meta_query' => array(
        array(
            'key'     => 'reviewed-user',
            'value'   => $user->ID,
            'compare' => '=',
        ),
    ),
        'posts_per_page'    => 1,
    ));
    return (count($result->posts));
      
}
add_shortcode( 'review_exists', 'check_review_exists' );

I wasn't sure whether to register to shortcode or the function in Toolset >> Settings >> Front-end Content, so I tried both.

I am using the Divi Builder to create the content template for Profiles. I tried a few different conditional statements such as this:

{!{wpv-conditional if="( '{!{wpv-current-user}!}' eq '{!{wpv-post-author}!}' )" evaluate="false"}!}

{!{wpv-conditional if="( check_review_exists() gt '0' )" evaluate="false"}!}

Leave a Review for this Supplier
{!{cred_form form="leave-a-review"}!}{!{/wpv-conditional}!}{!{/wpv-conditional}!}

I have logged in as a different user to the profile author, and have submitted a review, which I have published. However if I refresh the profile page while logged in as the other user, I still see the CRED form, which should now be hidden as a review does exist by the logged in user.

Can you see what I'm doing wrong?

#684133

Hello,

It is a custom codes problem, please check this:
1) Output the shortcode {!{review_exists}!} directly, check if it is does output the correct number count of current user.
2) Edit your codes as below:

{!{wpv-conditional if="( '{!{wpv-current-user}!}' ne '{!{wpv-post-author}!}' ) AND ( check_review_exists() eq 0 )" debug="true"}!}
Leave a Review for this Supplier
{!{cred_form form="leave-a-review"}!}
{!{/wpv-conditional}!}

And test again.

#685284

Hi Luo

Thanks man

Okay I've changed things slightly now. I now have a type 'Profile', with a child type 'Review'.

My code is now as follows:

function check_review_exists( ) {
      
    $user = wp_get_current_user();
	$post = get_post();
    $result = new WP_Query(array(
        'author'            => $user->ID,
        'post_type'     => 'review', 
        'post_status'       => 'publish',
        'meta_query' => array(
        array(
            'key'     => '_wpcf_belongs_profile_ID',
            'value'   => $post->ID,
            'compare' => '=',
        ),
    ),
        'posts_per_page'    => 1,
    ));
    return (count($result->posts));
      
}
add_shortcode( 'review_exists', 'check_review_exists' );

I have checked the output of the function. It works except when I use the meta query part. Have I referred to the post relationship properly?

I have checked that the $post->ID is correct, so I can only assume it is the key that is wrong? (I am trying to check if there are any review posts existing which are written by the current user which are children of the current displayed 'profile' post.

#689851

Please try to modify this line from:

'key'     => '_wpcf_belongs_profile_ID',

To:

'key'     => '_wpcf_belongs_profile_id',

And test again

#690541

Ah yes of course.

Thanks Luo, it all seems to be working now.

#690617

You are welcome.