Home › Toolset Professional Support › [Resolved] Require form to be filled for every post in a view
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 14 replies, has 2 voices.
Last updated by jamesR-13 2 years, 10 months ago.
Assisted by: Minesh.
I have a view that includes an edit form for each post in the view.
The posts are children of the current post being displayed.
I need to require the form to be submitted for every child post in the view before another form can be submitted that edits the parent post.
How can I accomplish this?
Hello. Thank you for contacting the Toolset support.
Can you please share problem URL and access details and tell me what child posts you want to fill out first and then how exactly you want to allow user to edit the parent edit form. Can you please share the whole workflow and admin access details and once I review your current setup I'll be able to guide you in the right direction.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
This will either require custom JS code or you custom PHP code where you validate the address form to make sure l each address has had at least one service selected, and for each service, the I agree to the terms checkbox checked, and the signature field filled in.".
Toolset offers form validation hook: "cred_form_validate" that you can use to validate your form.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
You can write custom JS code as per your requirement and add it to form's JS editor. if you will require assistance for such custom JS work, you can contact our certified partners at: https://toolset.com/contractors/
Or you can chose another way that, you should write a custom shortcode where you should try to find all child posts where the service is checked and term checkbox chekced and signature field is filled and get all those child posts and compare the total count and based on that you should show/hide "continue" button. To find all related child posts, you can use the post relationship API function:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
or
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/
Then you can use this custom shortcode with [wpv-conditoinal] shortcode and wrap the "Continue" button with it.
I would prefer to do it all server-side if possible. I have no budget for a contractor unfortunately.
Here's what I have done so far:
I created a custom shortcode that counts the number of child posts as such:
add_shortcode('address-count', 'address_count_func'); function address_count_func($atts) { global $post; $child_posts = toolset_get_related_posts($post->ID,'service-request-service-address','parent',999,0,array(),'post_id','child'); return count($child_posts); }
It does output the correct child post count and I have registered the shortcode to be used in conditionals(see attached image).
If you look here: hidden link
You can see that it correctly outputs 2.
However, I have added a conditional that should only be true if [address-count] is greater than or equal to 1:
[wpv-conditional if="( '[address-count]' gte '1' )"]
But the conditional doesn't seem to work as expected. I have tried not equal to zero as well with the same result, and even tried setting it to be true if equal to the exact known count of child posts, but it doesn't fire.
Question 1 is why isn't the conditional working with my custom shortcode?
This is of course leading back to solving the original question, which I intend to solve by counting child posts which have a particular field value set by cred_save_data as shown below:
I have already created the code to add the value to the field
if ($form_data['id']==372)//----------------------------------------------------------------------Service address - Set status of services selected { update_post_meta($post_id, 'wpcf-address-status', 'services selected'); }
Question 2 is once the conditional is working with my address-count shortcode, how would I count ONLY child posts with that custom field value using PHP?
Hopefully this is clear.
I appreciate your help Minesh.
I went ahead and took a stab at writing the code for question 1 above, but i messed up somewhere as it causes a critical error on my site when I call the shortcode. :/ Here's what I have:
add_shortcode('finished-address-count', 'services_selected_count_func'); function services_selected_count_func($atts) { $args = array( 'post_type' => 'Service Address', 'meta_key' => 'wpcf-address_status', 'meta_value' => 'services selected' ); $the_query = WP_Query($args); // The Loop if ( $the_query->have_posts() ) { $child_posts = toolset_get_related_posts($post->ID,'service-request-service-address','parent',999,0,array(),'post_id','child'); } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata(); return count($child_posts); }
What am I doing wrong?
OK, I am getting closer.
add_shortcode( 'get_count', 'get_count'); function get_count($atts) { $attributes = shortcode_atts( array( 'val' => '', ), $atts ); extract( $attributes); $args = array( 'post_type' => 'service-address', 'meta_key' => 'wpcf-address-status', 'meta_value' => $val, 'posts_per_page' => -1 ); $query = new WP_Query($args); return($query->found_posts); }
This code gets me a count of ALL addresses with the passed value, but now I need to limit it to child posts of the current post.
OK, so after further reading, I think this can be accomplished with the toolset_get_related_posts function so here's where i'm at:
add_shortcode( 'get-count', 'get_count'); function get_count($atts) { $addresses = toolset_get_related_posts( $request, 'service-request-service-address', [ 'query_by_role' => 'parent', 'role_to_return' => 'other', 'args' => [ 'meta_key' => 'wpcf-address-status', 'meta_value' => 'services selected', 'meta_compare' => 'LIKE', ], 'orderby' => 'title', 'order' => 'ASC' ] ); return count($addresses); }
Unfortunately, it crashes the site when I call the shortcode. What am I missing here?
OK, I got the related posts with meta figured out. Now I just need help figuring out why my conditional isn't working.
Here's my code for the shortcode:
add_shortcode( 'get-count', 'get_count'); function get_count($atts) { $addresses = toolset_get_related_posts( get_the_ID(),'service-request-service-address', 'parent',-1,0,array( 'meta_key' => 'wpcf-address-status', 'meta_compare' => 'LIKE', 'meta_value' => 'services selected', ), 'post_id','child','title','ASC' ); return count($addresses); }
You will have to register the shortcode "get-count" at:
- Toolset -> Settings page, click the Front-end Content tab and add your shortcode name in the Third-party shortcode arguments section.
I have two short codes, get-count and address-count, both are registered int he settings, but only get-count works with the conditional. Something is missing. Please advise.
That is strange. Can you please share problem URL where you added the shortcode and admin access details so I can dive into your site and review whats going wrong with your setup.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Nevermind, I had added them both under "Functions inside conditional evaluations" but not under "Third-party shortcode arguments". Once i added them there, they now both seem to be working as expected.
Thank you Minesh!
OK, i spoke too soon. Only one is working with the conditional. Please enable private replies again.
Also, i changed [get-count] to [finished-count], just so you are aware.
I have set the next reply to private which means only you and I have access to it.
I got it all working. I removed the shortcodes from the settings and re-added them, and everything started working.
My issue is resolved now. Thank you!