How to copy an order list from a category to another

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Customizing order of posts within categories is an easy task through Advanced Post Types Order plugin. The self explanatory interface featuring a drag & drop functionality makes sorting a breeze.

For large posts lists, order customize can take a bit of time, unless use automatic ordering. Manual sorting require drag & drop for individual posts in the list, up and down, Multiple Posts Selection can be done to speed up the process.

A category sorting (or any other taxonomy term) can be migrated to another one by a simple push of a button. This is useful when posts exists in booth terms and order is required to be the same, or similar (allow further adjustments).

There is a “Copy this sort to” option at the end of drag & drop list, which allow list order to apply o the other side.

To achieve this functionality, the following code is required. This can be placed inside theme functions.php or within a custom plugin.

add_action('apto/re-order-interface/footer-html','apto_reorder_interface_copy_sort');
    function apto_reorder_interface_copy_sort( $sort_view_id )
        {
            
            $sort_view_settings =  APTO_functions::get_sort_view_settings($sort_view_id);
            $sort_view_post     =  get_post($sort_view_id);
            $sort_ID            =  $sort_view_post->post_parent;
            
            
            ?>
            <style>
                .major-publishing-actions  p {margin: 0px}
                #apto_sort_copy img.process_image  {display: none} 
            </style>
            <div class="alignleft actions" id="apto_sort_copy"> 
                <p>Copy this sort to 
                <select name="apto_sort_copy_to" id="apto_sort_copy_to">
                    <?php
                    
                    $args  =  array(
                                        'post_type'    =>  'apto_sort',
                                        'post_parent'  =>  $sort_ID
                                        );
                    $custom_query      =  new WP_Query( $args );
                    
                    //make a sorted list of items
                    $sort_view_list =  array();
                    foreach($custom_query->posts    as  $found_sort_view_post)
                        {
                            $sort_view_post_details =  APTO_functions::get_sort_view_settings( $found_sort_view_post->ID );      
                            
                            if(empty($sort_view_post_details['_term_id']))
                                continue;
                                
                            if($sort_view_post->ID ==  $found_sort_view_post->ID)
                                continue;
                            
                            $term_data  =  get_term_by('id', $sort_view_post_details['_term_id'], $sort_view_post_details['_taxonomy']);
                            $sort_view_list[ $sort_view_post_details['_taxonomy'] ][ $found_sort_view_post->ID ]  =  $term_data->name;
                        }
                        
                    
                    foreach($sort_view_list    as  $taxonomy_name  =>  $data)
                        {
                            
                            asort($data);
                            
                            foreach($data  as  $sort_view_id    =>  $term_name)
                                {
                            
                                    ?>
                                    <option value="<?php echo $sort_view_id ?>"><?php echo ucfirst($taxonomy_name) ?> - <?php echo $term_name ?></option>
                                    <?php  
                                } 
                            
                        }
                    
                    ?>
                </select> <a href="javascript: void(0)" class="button-secondary" id="apto_sort_copy_button" onClick="apto_sort_copy_confirmSubmit()">Copy</a> <img alt="" src="<?php echo APTO_URL ?>/images/wpspin_light.gif" class="process_image"></p>
                
                <script type="text/javascript">
                                      
                    function apto_sort_copy_confirmSubmit()
                        {
                            var agree=confirm("Are you sure you want to copy this sort? All orders will be lost on the other side.");
                            if (agree)
                                {
                                    
                                    jQuery('#apto_sort_copy img.process_image').show();
                                    
                                    var queryString = { 
                                                    action:            'apto_sort_copy', 
                                                    sort_view_id:        sort_view_id,
                                                    copy_to:            jQuery('#apto_sort_copy_to').val(),
                                                    nonce:              '<?php echo wp_create_nonce( 'apto_sort_copy-' . get_current_user_id()) ?>'
                                                        };
                                    
                                    jQuery.ajax({
                                              type: 'POST',
                                              url: ajaxurl,
                                              data: queryString,
                                              cache: false,
                                              dataType: "html",
                                              success: function(response){
                                                jQuery('#apto_sort_copy img.process_image').hide();
                                                alert('Sort successfully copied');
                                              },
                                              error: function(html){
                                                jQuery('#apto_sort_copy img.process_image').hide();
                                                  }
                                            });    
                                }
                                else
                                {
                                    return false ;
                                }
                        }
                            
                </script>
                
            </div>
            
            <?php  
            
            
            
        }
        
    add_action( 'wp_ajax_apto_sort_copy', 'wp_ajax_apto_sort_copy' );
    function wp_ajax_apto_sort_copy()
        {
            
            //check for nonce
            if(! wp_verify_nonce($_POST['nonce'],  'apto_sort_copy-' . get_current_user_id()))
                {
                    _e( 'Invalid Nonce', 'apto' );
                    die();  
                }  
            
            $sort_view_id  =  $_POST['sort_view_id'];
            $copy_to        =  $_POST['copy_to'];
            
            global $wpdb;
            
            $query = "DELETE FROM `". $wpdb->prefix ."apto_sort_list`
                            WHERE `sort_view_id`    =  ". $copy_to;
            $results = $wpdb->get_results($query);
            
            //retrieve the sort list
            $query  =  "SELECT object_id FROM `". $wpdb->prefix ."apto_sort_list`
                            WHERE sort_view_id  = '". $sort_view_id ."'";
            $results        =  $wpdb->get_results( $query );
            
            if(count($results)  <  1)
                die();
            
            $query = "INSERT INTO `". $wpdb->prefix ."apto_sort_list` 
                        (`sort_view_id`, `object_id`) 
                        VALUES ";
            
            $sort_list  =  array();
            $first      =  TRUE;
            foreach($results    as  $result)
                {
                    
                    if($first  === FALSE)
                        $query  .=  ', ';
                        
                    $query  .= " ('" . $copy_to . "', '" . $result->object_id . "') ";
                    
                    $first  =  FALSE;
                    
                }
                    
            $results = $wpdb->get_results( $query );
            
            die();
            
        }