Skip Navigation

[Resuelto] Bulk update of selected records not updating all selected ust the last selected

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I have set up a custom form where my site users can select multiple posts. When the form is submitted, the page URL is updated to include those selected post IDs in a repeating URL parameter post_ids. I have custom code in place that triggers a cred_save_data hook when this page loads, and the code grabs post_ids from the URL parameter to update those posts. Multiple post IDs are added to the URL successfully, but only one post is actually updated by the cred_save_data hook.

Solution: To access URL parameters in a cred_save_data hook, find them in $_GET instead of $_POST:

$url_params = $_GET['post_ids'];

Also be sure to use the square bracket notation to indicate multiple values are included:

https://yoursite.com/delegate-update-results/?post_ids[]=44175&post_ids[]=44163&post_ids[]=44162

Then you must update your code to work with an array of values instead of a comma-separated string.

This support ticket is created hace 4 años, 3 meses. 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Etiquetado: ,

This topic contains 2 respuestas, has 2 mensajes.

Last updated by mikeB-14 hace 4 años, 3 meses.

Assisted by: Christian Cox.

Autor
Mensajes
#1456285
selectdelegates.JPG
bulkupdateemailrequest.JPG

Hi Guys
I have had this problem for a while still have not cracked it yet
So the select delegate allows you to select the delegates you want to update

hidden link
as you can see the info is passed in the url

Once that is selected it passes the url parameters ob the bulk update form and you click the update button and it should update the
emailrequest field for each selected record.

hidden link

This is the url that comes back from clicking the update

My code snippet is this.

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

/**
 * Define the action and give functionality to the action.
 */
 function cred_save_data_42932() {
   do_action( 'cred_save_data_42932' );
 }



add_action('cred_save_data_42932', 'my_update_email_request_data_action_42932',10,2);
function my_update_email_request_data_action_42932($post_id, $form_data)
{
  
   // if a specific form
    if($form_data['id']==42932) //Change to the Form ID you use here (it's 42847)
    {
        //get data to update with - you will need to change this to your custom field or fields, if you have many.
        $data_to_update_with = $_POST['wpcf-email-request'];
       
       
        //get data from post_ids parameters in URL - no change should ne needed here unless the attribute changes (it is post_ids here)
        $url_params = $_POST['post_ids'];

        //remove & - we need this to later easily create an array of all post_ids passed
       //note, it would be wise to check here if you actually HAVE some post_ids or not, but this is a refinement to be done later
      $url_params = str_replace('&', ' ',$url_params);
  
        //split into array - the post_ids are many, so we need to ahve them each on it's own
        $url_params = explode('post_ids=',$url_params);
      
        //At this point we have an array of Post ID's we want to edit, as passed in post_ids url arguments, in an array
        //now we can update all the posts in that array!
        foreach ($url_params as $post => $id) {
     
            //update only if Post ID is not 0
         
            if ($id != 0 && !empty($id)){
                        //update the field you need to update, in this case a single line Types field, with the data you got from the Form, see first lines of the code above
                update_post_meta($id, 'wpcf-email-request', $data_to_update_with);
         
            }
        }
    }
}

I would like to get the bulk update loop working , not sure why it's not working

Thanks
Tony

#1456789

Hi, if you are accessing the post_ids variable from URL parameters, then you need to use $_GET instead of $_POST here:

$url_params = $_GET['post_ids'];

You should also use the array bracket notation in the URL:

<em><u>hidden link</u></em>

Otherwise, each post_ids will just override the previous one and never give you an array like you want. Then you'll probably have to update the rest of your code to work with an array of post_ids.

#1468955

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.