0

I had created a custom field using ACF in a quick edit. A field is showing where a user has to update the menu order of the product. When you see the product details page by clicking the edit button that field will show in an advance tab, menu order. Now the user wants to change the menu order from the quick edit field. I am using ajax to achieve but I am stuck for 4 hours. I can't find any help on the internet. Check my code below. Thank

JQuery Code:

jQuery(document).ready(function(){

jQuery(".acf-quick-edit input[type='text']").blur(function(){
    var morder = jQuery(this).val();
    var post_id = jQuery(this).parents("tr").attr("id");

    var data = {
        'action': 'update_menu_order',
        'menu_order': morder,
        'post_id':post_id
    };

    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
    });


});

});

PHP Code

<?php 

add_action( 'wp_ajax_update_menu_order', 'update_menu_order' );
add_action( 'wp_ajax_nopriv_update_menu_order', 'update_menu_order' );

function update_menu_order() {
    global $wpdb; // this is how you get access to the database

    $menu_order = $_POST['menu_order'];
    $data = $_POST['post_id'];
    $post_id = substr($data, strpos($data, "-") + 1);    
//print_r($_POST);
//echo get_post_field( 'menu_order', $post_id);
update_post_meta($post_id,'menu_order',$menu_order);

    

    wp_die(); // this is required to terminate immediately and return a proper response
}
0

The below code will help others to achieve the above task.

<?php 
add_action( 'wp_ajax_update_menu_order', 'update_menu_order' );
add_action( 'wp_ajax_nopriv_update_menu_order', 'update_menu_order' );

function update_menu_order() {
global $wpdb; 
$table_name = $wpdb->prefix."posts";

$menu_order = $_POST['menu_order'];
$data = $_POST['post_id'];
$post_id = substr($data, strpos($data, "-") + 1);    

$result = $wpdb->query($wpdb->prepare("UPDATE $table_name SET menu_order='$menu_order' WHERE ID=$post_id"));

echo $result;


wp_die(); // this is required to terminate immediately and return a proper response
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.