Skip Navigation

[Resolved] Form to update multiple records

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.

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 1 reply, has 2 voices.

Last updated by Minesh 9 months, 2 weeks ago.

Assisted by: Minesh.

Author
Posts
#2682398

Hi

I'd like to know how I can make a form to be able to change the value of multiple instances at once.

For example, one of the field of my CPT "property" is a checkbox "carousel" to indicate if the property need to be displayed in the front page carousel.

I'd like to display a page with all the properties names with the checkbox beside, change the values of the ones I need to change and click on 1 button "save" to update all the new values. instead of changing them one by one

Is it possible ? Even if I display for eventual changes more than one field ?

#2682438

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There is no feature available to update multiple post values using the single form.

But I could provide workaround at some extent that should help you at least.

Note:
- If you are using Toolset Blocks plugin and you do not have option "Views" to create view in classic/legacy mode from Toolset => Views, then you should enable the classic/legacy version of the view by following the following Doc:
=> https://toolset.com/course-lesson/enabling-legacy-version-of-toolset-views/

Here is the sandbox site and you can auto-login to it using the following link:
- hidden link
Where:
I've custom checkbox field "Featured Property" with every "Property" post.

Can you please try to follow the following steps:

1) Create new post with post title "Test post" with your "property" post type and set the post status as "draft"
As you can see here is the "draft" post I created with sandbox site:
- hidden link

2) Create a view in classic mode for post type "property" and setup "Loop Editor" as you can see with the following link:
- hidden link

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
 <ul class="wpt-form-set wpt-form-set-checkboxes wpt-form-set-checkboxes-set-featured-property">
		<wpv-loop>
         
<li class="wpt-form-item wpt-form-item-checkbox checkbox-4-bedroom-house-share">
<input type="checkbox" id="[wpv-post-id]" name="set-featured-property[]" data-wpt-type="checkbox" data-wpt-id="cred_form_2493_1_1_[wpv-post-id]" data-wpt-name="set-featured-property[]" value="[wpv-post-id]" data-value="[wpv-post-id]" class="wpt-form-checkbox form-checkbox checkbox" 
       [wpv-conditional if="( $(wpcf-carousel) eq '1' )"] checked [/wpv-conditional]
       >
  
  		<input type="hidden" id="hidden-[wpv-post-id]"  name="hidden-set-featured-property[]" value="[wpv-post-id]" />
  <label class="wpt-form-label wpt-form-checkbox-label" for="[wpv-post-id]">[wpv-post-title]</label>
</li>
			
		</wpv-loop>
   </u>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Where:
- I already replaced"featured-property" with "carousel" which should be your custom field slug.

3) Create a post edit form for post type "property" and add the above view inside the form:
- hidden link

[credform]
	[cred_field field='form_messages' class='alert alert-warning']

<div class="form-group">
 <label for="%%FORM_ID%%_featured-property">[cred_i18n name='featured-property-label']Featured Property[/cred_i18n]</label>
[wpv-view name="show-all-properties-on-form"] 
</div>
<hr />	
	[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

4) Now, create a new page and add the above form to it and set the post form to edit the "Test post" which is a "draft" post
- hidden link

5) Add the following custom code to "Custom Code" section offered by Toolset:
=> hidden link

add_action('cred_save_data','func_set_status_for_multiple_posts',10,2);
function func_set_status_for_multiple_posts($post_id,$form_data ) {
  	$field_slug = 'featured-property';
    
  if ($form_data['id']==2493) {
         
      if(isset($_POST['set-featured-property']) and !empty($_POST['set-featured-property']) ) {
        
      $all_checked = $_POST['set-featured-property'];
      foreach($all_checked as $k=>$v):
        	update_post_meta($v,'wpcf-'.$field_slug,1);
     endforeach;
        
     $all_unchecked = array_diff($_POST['hidden-set-featured-property'],$_POST['set-featured-property']);
        
    	foreach($all_unchecked as $k=>$v):
        	delete_post_meta( $v,'wpcf-'.$field_slug, null );
     	endforeach;
        
      
      }
      
    }
}

Where:
- You can replace the 2493 with your original edit form ID
- Replace the $field_slug value with your original custom field slug, for you it could be "carousel".

6) Load the page where you added the form on frontend and try to select "Featured Property" status and submit the form and check its saved correctly in the backend.
- hidden link

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/legacy-features/views-plugin/front-page-filters/
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://toolset.com/course-lesson/front-end-forms-for-editing-content/#1-create-the-form-for-editing

I hope the solution I shared will help you to address the issue exactly you are looking for. Please let me know how it goes.