[Resolved] Conditional display of CRED form on post depending if a child post exists

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 answer your ticket in about 2.78 hours from now. Thank you for your understanding.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

This topic contains 5 replies, has 2 voices, and was last updated by  julienL-5 1 week, 1 day ago. The support staff assigned to this topic is Noman.

Viewing 6 posts - 1 through 6 (of 6 total)
Author
Posts
#575548

Hello guys,

I'm trying to implement a "Recommend" button on some post templates via a CRED form, a bit similar to a "Like" button, so users can recommend posts in 1 click.

I have managed to do it almost 100% through a post relationship with Recommendations as CPT having as parents both the CPT recommended (let's call it "Resource") and a "Profile" CPT. It all works well and clicking this button (CRED form with all fields hidden) creates a new Recommendations post with the Resource viewed as a parent and the Profile of the user as the other Parent.

But I'm now trying to have a conditional display on the post as such:
- if the user has not recommended the post, the CRED form is shown
- if the user has recommended the post, the CRED form is not shown

To achieve that, my approach is to configure the conditional display rules as follows:
- if a "Recommendation" exists as a child post of the current Resources post displayed, with the author being the logged-in user, then the CRED form is not displayed

I have found in the forum a way to achieve this partially by defining and registering a shortcode as follows:

add_shortcode( 'child-post-by-author-exist', 'child-post-by-author-exist' );
function child-post-by-author-exist( $atts ) {
    $child_posts = types_child_posts('resources');
    if ($child_posts) return true;
}

This returns true if child posts exists. What I would like to add is a condition where this returns true only if child post by the logged-in user exists.

I have tried a couple of different modifications, but I think I'm way off the mark here 🙁

Would you be able to help with this?

Many thanks!

Best,
- Julien

#575707

Noman
Forum moderator

Supporter languages: English (English )

Supporter Timezone: Asia/Karachi (GMT+05:00)

Hi Julien,

Thank you for contacting Toolset support.

1. Please provide link to the Form page where you have added Recommend button.

2. But I'm now trying to have a conditional display on the post as such:
- if the user has not recommended the post, the CRED form is shown
- if the user has recommended the post, the CRED form is not shown
>> Please provide more details that which form should display and where? Please provide some screenshots as well.

3. Please provide an example and expected output for Recommended Post and an example for Not recommended post.

Thank you

#576995

Hi Noman -

Many thanks for your reply and my sincere apologies for the delay, I was off sick 🙁

You can see an example of the form on this page: https://parkourbase.com/book/parkour-strength-training-overcome-obstacles-for-fun-and-fitness/

I have also uploaded 2 screenshots:
- one showing the form on the page
- one showing the form settings

You'll see there are 3 CPTs at play here:
- practitioner: this is a CPT used as a profile for my users. Users can only create 1 practitioner post, and the id of their practitioner post is stored as a custom field in their custom user fields for easy retrieval
- books: this is a CPT used to display books that practitioners can recommend
- book recommendations: this is a CPT that is a child of both previous CPTs and that is used to link practitioners to the books they'd recommend

The form creates a new "Book recommendation" post, and it assigns as parent the practitioner id stored in the custom field of the logged-in user and it also assigns as parent the book id of the page currently displayed. So clicking the submit button of the form (which has the value "Recommend") creates the correct Book Recommendation CPT.

The recommendation will then be displayed both in the book post, showing which practitioners are recommending the book, and in the practitioner post, to show which books a practitioner has recommended.

My problem here is that once a user has recommended a book, I want to hide the form, since users should only be able to recommend a book once. Currently, the form will continue to be displayed on the page, which is confusing for the users.

That is what I was trying to explain in my initial post 🙂

Please do let me know if this is now clear or if you have additional questions!

Best,
- Julien

#577200

Noman
Forum moderator

Supporter languages: English (English )

Supporter Timezone: Asia/Karachi (GMT+05:00)

Hello Julien,

Sorry we ran into the weekend. Thanks for providing details, I am going to look into this and will update you with my findings shortly.

Thank you for you patience.

#577356

Noman
Forum moderator

Supporter languages: English (English )

Supporter Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting on this. The following code will return true if child post by the logged-in user exists.

1. Please add this code in your theme’s or child theme’s functions.php file:

function check_child_post_by_author_exist_func( ) {
	
	$user = wp_get_current_user();
	$result = new WP_Query(array(
		'author'			=> $user->ID,
		'post_type'		=> 'book-reco', // your child CPT slug
		'post_status'		=> 'publish',
		'posts_per_page'	=> 1,
	));
	return (count($result->posts)!=0);
	
}
add_shortcode( 'check_child_post_by_author_exist', 'check_child_post_by_author_exist_func' );

[/php]
==> Whereas ''book-reco' will need to be replaced with your child post_type slug.

2. Register shortcode first 'check_child_post_by_author_exist' in Toolset >> Settings >> Front-end Content >> Third-party shortcode arguments.

3. Then you can use shortcode in the View like this:

[wpv-conditional if="( '[check_child_post_by_author_exist]' ne true )"]
Content goes here
[/wpv-conditional]

I hope it helps, thank you

#578054

Hi Noman,

Many thanks for this! I could build on your code to achieve what I wanted (which may not have been clear form my initial post). Here's my updated code for reference and what I changed.

1. Shortcode code
Your code was just missing one thing: I needed to filter the query to only the child posts of the currently displayed parent post, so I modified your code as follows:

function check_child_post_by_author_exist_bookreco_func( ) {
     
    $user = wp_get_current_user();
    $bookid = get_queried_object_id();
    $result = new WP_Query(array(
        'author'            => $user->ID,
        'post_type'     => 'book-reco', // your child CPT slug
        'post_status'       => 'publish',
        'meta_query' => array(
        array(
            'key'     => '_wpcf_belongs_book_id',
            'value'   => $bookid,
            'compare' => 'LIKE',
        ),
    ),
        'posts_per_page'    => 1,
    ));
    return (count($result->posts));
     
}
add_shortcode( 'check_child_post_by_author_exist_bookreco', 'check_child_post_by_author_exist_bookreco_func' );

2. Conditional display code
For a reason I never understood, your shortcode was working great when evaluating to not true, but displayed nothing when evaluating to true. So I changed your code for the shortcode (above) to return the actual count and not a boolean, and I'm using the conditional display to check on the number.

I also wanted to have a conditional display based on whether or not a user is logged-in, which is in my code below:

[wpv-conditional if="('[wpv-current-user info='id']' eq '')"]
Not logged-in
[/wpv-conditional]
[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' eq '')"]
[wpv-conditional if="( '[check_child_post_by_author_exist_bookreco]' = 0 )"]
[cred_form form='new-reco-book' form_name='New Book Recommendation']
[/wpv-conditional]

[wpv-conditional if="( '[check_child_post_by_author_exist_bookreco]' = 1 )"]
Already recommended!
[/wpv-conditional]

[/wpv-conditional]

Thanks again so much for your great assistance 🙂

Best,
- Julien

Viewing 6 posts - 1 through 6 (of 6 total)

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.