• Hi,

    I have a submenu admin section under my custom post type which lists the posts. I have applied jQuery sortable and it all works fine except for the actual saving to the postmeta in the database.

    <?php
    /*
    Plugin Name: Order
    Description: Posts Order using a Drag and Drop Sortable javascript capability
    */
    
    define('SORTPATH',   plugin_dir_path(__FILE__));
    define('SORTURL',    plugins_url('', __FILE__));
    
    add_action( 'admin_init','registerFiles');
    function registerFiles()
                {
    
                            wp_enqueue_script('jQuery');
                            wp_enqueue_script('jquery-ui-sortable');
    
                    wp_register_style('CPTStyleSheets', SORTURL . '/css/cpt.css');
                    wp_enqueue_style( 'CPTStyleSheets');
    	        }
    
    add_action( 'wp_ajax_update-custom-type-order','saveAjaxOrder');
    function saveAjaxOrder()
                {
    		        global $wpdb;
    
    		        parse_str($_POST['order'], $data);
    
    		        if (is_array($data))
                    foreach($data as $key => $values )
                        {
    
    				                foreach( $values as $position => $id )
                                        {
    					                    $data = array('menu_order' => $position, 'post_parent' => 0);
                                            $data = apply_filters('post-types-order_save-ajax-order', $data, $key, $id);
    
                                            update_post_meta($id, 'asset-order', $position);
    				                    } 
    
    		            }
    	        }
    
    add_action( 'admin_menu','addMenu');
    	    function addMenu()
            {
    
    	add_submenu_page(
    		'edit.php?post_type=staff',
    		'Order',
    		'Order',
    		'edit_pages', 'staff-order',
    		'SortPage'
    	);
    }
    function AssetPage()
    {
           wp_reset_query();
        query_posts(array(
            'post_type' => 'staff',
            'categories' => 'asset-advisory-services',
            'orderby' => 'menu_order',
            'order' => ASC
        ) );  
    
    echo '<ul class="sortable">';
    if ( have_posts() ) : while ( have_posts() ) : the_post(); 
    
        echo '<li>';
        echo '<i class="flaticon-move7"></i>';
        the_title();
        echo '</li>';
    
    endwhile; else:
    endif;
    echo '</ul>';
    }
    function SortPage()
                {
    		        ?>
    		        <div class="wrap">
                        <h2><?php echo __('Staff Order', 'cpt') ?></h2>
    
    			        <div id="ajax-response"></div>
    
    			        <noscript>
    				        <div class="error message">
    					        <p><?php _e('This plugin can\'t work without javascript, because it\'s use drag and drop and AJAX.', 'cpt') ?></p>
    				        </div>
    			        </noscript>
    
    			        <div id="asset" style="background:white;">
    				        <h4>Asset Page</h4>
    			                 <?php AssetPage(); ?>
    
    				        <div class="clear"></div>
                            <button id="save-order">Save</button>
    			        </div>
    
    			        <script type="text/javascript">
    				        jQuery(document).ready(function() {
    					        jQuery(".sortable").sortable({
    						        'tolerance':'intersect',
    						        'cursor':'move',
    						        'items':'li',
    						        'placeholder':'placeholder',
    						        'nested': 'ul'
    					        });
    
    					        jQuery(".sortable").disableSelection();
    					        jQuery("#save-order").bind( "click", function() {
    						        jQuery.post( ajaxurl, { action:'update-custom-type-order', order:jQuery(".sortable").sortable("serialize") }, function() {
    							        jQuery("#ajax-response").html('<div class="message updated fade"><p><?php _e('Items Order Updated', 'cpt') ?></p></div>');
    							        jQuery("#ajax-response div").delay(3000).hide("slow");
    						        });
    					        });
    				        });
    			        </script>
    
    		        </div>
    		        <?php
    	        }
    
    ?>

    I have no idea where I’m going wrong here.

The topic ‘Ajax URL not saving’ is closed to new replies.