[Resolved] Conditional display of CRED form on post depending if a child post exists
This thread is resolved. Here is a description of the problem and solution.
Problem:
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.
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.
This support ticket is created 6 years, 11 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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 🙁
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.
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: hidden link
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!
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:
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: