skip to Main Content

I’m adding some features to the quck edit area of wordpress. I have everything working except updating post_content. I am able to update the meta values using update_post_meta, but the method I’m using to update post_content just causes the infinite spinning wheel.

function min_max_step_desc_quick_edit_save( $post_id, $post ){

    // check inlint edit nonce
    if ( ! wp_verify_nonce( $_POST[ '_inline_edit' ], 'inlineeditnonce' ) ) {
        return;
    }

    // update the price
    $min = ! empty( $_POST[ 'min' ] ) ? absint( $_POST[ 'min' ] ) : '';
    update_post_meta( $post_id, '_alg_wc_pq_min', $min );

    $max = ! empty( $_POST[ 'max' ] ) ? absint( $_POST[ 'max' ] ) : '';
    update_post_meta( $post_id, '_alg_wc_pq_max', $max );
    
    $step = ! empty( $_POST[ 'step' ] ) ? absint( $_POST[ 'step' ] ) : '';
    update_post_meta( $post_id, '_alg_wc_pq_step', $step );
    
    $desc = ! empty( $_POST[ 'desc' ] ) ? absint( $_POST[ 'desc' ] ) : '';
    $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_content'] = $desc;
        wp_update_post( $my_post );
}

2

Answers


  1. Try to reduce the error by doing the following:

    function min_max_step_desc_quick_edit_save( $post_id, $post ) {
        // Update post
        $my_post = array(
            'ID'           => $post_id,
            'post_title'   => 'This is the post title.',
            'post_content' => 'This is the updated content.',
        );
    
        // Update the post into the database with $wp_error to true
        wp_update_post( $my_post, true );
    
        // Of course, this should be done in an development environment only and commented out or removed after deploying to your production site.                
        if ( is_wp_error( $post_id ) ) {
            $errors = $post_id->get_error_messages();
            foreach ($errors as $error) {
                echo $error;
            }
        }
    }
    
    Login or Signup to reply.
  2. As this is for WooCommerce Products Quick Edit, you can use the following instead:

    // Save quick edit custom fields values
    add_action( 'woocommerce_product_quick_edit_save', 'min_max_step_desc_quick_edit_save' );
    function min_max_step_desc_quick_edit_save( $product ){
        $updated = false;
    
        if ( isset($_REQUEST['min']) ) {
            $product->update_meta_data('min', (!empty($_REQUEST['min']) ? intval($_REQUEST['min']) : ''));
            $updated = true;
        }
    
        if ( isset($_REQUEST['max']) ) {
            $product->update_meta_data('max', (!empty($_REQUEST['max']) ? intval($_REQUEST['max']) : ''));
            $updated = true;
        }
    
        if ( isset($_REQUEST['step']) ) {
            $product->update_meta_data('step', (!empty($_REQUEST['step']) ? intval($_REQUEST['step']) : ''));
            $updated = true;
        }
    
    
        // For the product description ("post_content")
        if ( isset($_REQUEST['description']) ) {
            $product->set_description(!empty($_REQUEST['description']) ? sanitize_text_field($_REQUEST['description']) : '');
            $updated = true;
        }
    
        // Save updated product data and metadata to database
        if ( $updated ) {
            $product->save(); // save product meta data
        }
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search