I'm having some trouble with a custom thing I'm doing.
I've implented a custom image uploader into a metabox in a custom post type. Now I'm trying to work out a way that will let me reorder the uploaded images. I found that jQuery Sortable would be a convenient way for the user to be able to just drag and drop in which order the images should appear (in the frontend I'm using a jQuery image slider to show the images and the order of the images are decided by the attachment data "menu_order", so therefor I need some sort of functionality that saves the menu_order that I create with the jQuery Sortable.
Now, this is a simplified example of what I have: http://jsfiddle.net/LN4sA/
Together with each attachment I have added hidden input fields with the attachment id and a field that will house the current position of the attachment in the menu order.
The ID of the attachment is easily fetched with basic WP PHP ($attachment->ID). However, I have not yet found an easy way to populate the #att_order with the proper menu_order. So here I'd like some help/input if someone would be so kind.
To save the actual information I'm using ajax to pull the values from the input fields:
add_action('save_post', 'save_attachment_position');
function save_attachment_position(){
global $post;
if ($post->post_type == 'work') { ?>
<script type="text/javascript">
//<![CDATA[
jQuery.ajax({
type: 'post',
url: ajaxurl,
data: {
action: 'order_attachment',
att_ID: jQuery(this).parents('.attachment').find('#att_id').val(),
att_order: jQuery(this).parents('.attachment').find('#att_order').val(),
_ajax_nonce: jQuery('#ordernonce').val(),
post_type: 'attachment'
}
});
//]]>
</script><?php
}
}
And then use wp_update_post to update the attachment data:
add_action('wp_ajax_order_attachment', 'order_attachment');
function order_attachment($post) {
$attachmentdata = array();
$attachmentdata['ID'] = $_POST['att_ID'];
$attachmentdata['menu_order'] = $_POST['att_order'];
wp_update_post($attachmentdata);
}
I'm aware that I need some sort of loop here but I will work that out.
Any ideas?
Thanks