skip to Main Content

Edit——–
Problem solved, see my reply below for the code!

Sorry for bad English.
I’m making changes to a theme in order to provide the ability to make bulk changes from the dashboard front-end.
I’ve already managed to provide the same functionality for individual posts, through simple buttons, a function and a little bit of JS.

Now I would like to do the same thing in bulk, but if before reading the IDs of each post it was the button, now it must be a checkbox, which will have to return as an array of IDs to the function.

I did some tests and the code works, not totally.

Because now I can perform the action, but only for a single post, even if I check all post, it performs the action only for the first post checked.

I guess it’s a write error in the function in php, I should pass it the propID values ​​as an array, or do a foreach for each (lol) post. But I didn’t quite understand how to do it, do you have any suggestions?

This is my code:

button:

 <button type="button" class="multi-on-hold btn btn-primary btn-sm">Premi</button>

checkbox:

<input type="checkbox" class="multicheck_prop" name="multicheck_prop[]" value="<?php echo ($post->ID); ?>">

function:

add_action( 'wp_ajax_multicheck_houzez_on_hold', 'multicheck_houzez_on_hold' );

if ( !function_exists( 'multicheck_houzez_on_hold' ) ) {
    function multicheck_houzez_on_hold() {

        if ( isset( $_POST['propID'] ) ) {

            global $wpdb;
            if (! isset( $_POST['propID'] ) ) {
                wp_die('No post to put on hold has been supplied!');
            }
            $post_id = absint( $_POST['propID'] );
            
            $post_status = get_post_status( $post_id );

            if($post_status == 'publish') { 
                $post = array(
                    'ID'            => $post_id,
                    'post_status'   => 'on_hold'
                );
            } elseif ($post_status == 'on_hold') {
                $post = array(
                    'ID'            => $post_id,
                    'post_status'   => 'publish'
                );
            }
            $post_id =  wp_update_post($post);
        }
            return true;
    }
}


js: 

    

            $('.multi-on-hold').on( 'click', function( e ) {
            e.preventDefault();
            var $this = $( this );
            var propid = $this.data( 'property' );
            var checkboxVals = $('.multicheck_prop');
    
            var vals = $('.multicheck_prop:checked').map(function(){return this.value;}).get().join(',')
    
            if(vals == "") {
                return;
            }
            
            $.ajax({
                
                url: ajax_url,
                data: {
                    'action': 'multicheck_houzez_on_hold',
                    'propID': vals,
                },
                method: 'POST',
                dataType: "JSON",
                
                beforeSend: function( ) {
                    //window.alert(vals)
                    houzez_processing_modal(processing_text);
                },
                success: function( response ) {
                    window.location.reload();
                },
                complete: function(){
                }
            });               
        });

3

Answers


  1. Chosen as BEST ANSWER

    Issue Solved! I've changed a piece of code to recognize the 'ID' in array and implement the solution of @Suver1.

    Thank you for the suggestions!

    This is my working code:

    $('.multi-on-hold').on( 'click', function( e ) {
                e.preventDefault();
                var $this = $( this );
                var propid = $this.data( 'property' );
                //var Nonce = $this.data('nonce');
        
                var checkboxVals = $('.multicheck_prop');
                //var vals = $('.multicheck_prop:checked').map(function(){return this.value;}).get().join(',')
                var vals = $('.multicheck_prop:checked').map(function(){return this.value;}).toArray();
        
                if(vals == "") {
                    return;
                }
                
                $.ajax({
                    
                    url: ajax_url,
                    data: {
                        'action': 'multicheck_houzez_on_hold',
                        'propID': vals,
                    },
                    method: 'POST',
                    dataType: "JSON",
                    
                    beforeSend: function( ) {
                        //window.alert(vals), // See the value of any ID
                        houzez_processing_modal(processing_text);
                    },
                     success: function( response ) {
                        window.location.reload();
                    },
                    complete: function(){
                    }
                });
                   
            });
    if ( !function_exists( 'multicheck_houzez_on_hold' ) ) {
        function multicheck_houzez_on_hold() {
            if ( isset( $_POST['propID'] ) ) {
            global $wpdb;
            if (! isset( $_POST['propID'] ) ) {
                wp_die('No post to put on hold has been supplied!');
            }
                // convert propID to array if it isnt -- Thanks to Suver1
                $post_ids = !is_array($_POST['propID']) ? [$_POST['propID']] : $_POST['propID'];
                foreach ($post_ids as $post_id)  {
                    $post_status = get_post_status( $post_id );
                    if($post_status == 'publish') { 
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'on_hold'
                        );
                    } elseif ($post_status == 'on_hold') {
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'publish'
                        );
                    }
                    $post_id =  wp_update_post($post);
                } // End foreach
            } // End IF
            return true;
        } // End this function
    } // End function exists


  2. The issue is in checkbox name multicheck_prop[] you define as an array so you have to access as an array. check below code.

    add_action( 'wp_ajax_multicheck_houzez_on_hold', 'multicheck_houzez_on_hold' );
    
    if ( !function_exists( 'multicheck_houzez_on_hold' ) ) { 
        function multicheck_houzez_on_hold() {
    
            global $wpdb;
    
            if (! isset( $_POST['propID'] ) ) {
                wp_die('No post to put on hold has been supplied!');
            }
    
            if( !empty( $_POST['propID'] ) ){
    
                foreach ( (array) $_POST['propID'] as $key => $propID ) {
                    
                    $post_id = absint( $propID );
                    
                    $post_status = get_post_status( $post_id );
    
                    if($post_status == 'publish') { 
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'on_hold'
                        );
                    } elseif ($post_status == 'on_hold') {
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'publish'
                        );
                    }
                    $post_id =  wp_update_post($post);
                }
                wp_send_json_success();
            }
    
            wp_send_json_error();
        }
    }
    
    Login or Signup to reply.
  3. Since you want to send many IDs to PHP, you can just loop over them as you pointed out.
    This would support both one or many ids:

    add_action( 'wp_ajax_multicheck_houzez_on_hold', 'multicheck_houzez_on_hold' );
    
    if ( !function_exists( 'multicheck_houzez_on_hold' ) ) { 
        if (isset($_POST['propID'])) {
    
                global $wpdb;
                if (!isset($_POST['propID'])) {
                    wp_die('No post to put on hold has been supplied!');
                }
                // convert propID to array if it isnt
                $prop_ids = !is_array($_POST['propID']) ? [$_POST['propID']] : $_POST['propID'];
    
                foreach ($prop_ids as $prop_id) {
                    $post_id = absint($prop_id);
    
                    $post_status = get_post_status($post_id);
    
                    if ($post_status == 'publish') {
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'on_hold'
                        );
                    } elseif ($post_status == 'on_hold') {
                        $post = array(
                            'ID'            => $post_id,
                            'post_status'   => 'publish'
                        );
                    }
                    $post_id =  wp_update_post($post);
                }
            }
            return true;
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search