Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've managed create a metabox that lets me upload and then fetch the images and then also been able to add jquery ui sortable to be able to sort the images, however I've no idea how to add actual functionality so that the sort order get saved.

If someone has an idea how to achieve this or some tutorial that would be helpful, please share, I'm helplessly stuck here.

Thanks.


Edit: Basically, what I need to know is how to tie jQuery sortable together with the functionality that handles the different attachments menu_order.

share|improve this question
    
Note: I don't know what exactly you use to upload images from your meta box, but I guess you did stick with that most reknown tutorials and enritched your metabox custom field to use the media uploader. If the below answer doesn't fit, than you've been too less specific in your Q and didn't put in enough effort to let us know what we're dealing with. In this case, please go back and update your Q. –  kaiser Jul 16 '11 at 15:53
    
@kaiser Sorry for being a bit vague in my question. I've updated the Q with some more information. I'm not using the regular media uploader. I'm using Uploadify that ties in with the media library, though it works exactly the same as it would with the regular media uploader. So what I need is someway to use the menu order that I create with jQuery Sortable (check this video: youtu.be/zvPnem6ady4) and then save it as a menu_order ID for my different attachments. Am I making any sense? –  INT Jul 16 '11 at 16:06
    
Line 1228 in media.php in wp-admin/includes seems to have the functionality for how sorting natively in WP works. Not sure how to make use of it though.. –  INT Jul 16 '11 at 16:30
    
The answer below shows you how to sort in your tempaltes after you got around this one. Sorry, but I ain't have the time now to solve your task. –  kaiser Jul 16 '11 at 16:56
add comment

3 Answers

up vote 1 down vote accepted

To save the menu order use wp_update_post() with each attachment ID and its new menu position:

$thisattachment = array();
$thisattachment['ID'] = $thisid;
$thisattachment['menu_order'] = $new_menu_position;
wp_update_post($thisattachment);

EDIT - assuming these are already attachments and you're updating them? Otherwise you have to insert the attachments first with wp_insert_attachment

share|improve this answer
    
This seems to be what I want, will try as soon as I get a chance, thanks! –  INT Jul 18 '11 at 8:43
    
worked perfectly, thanks! –  INT Jul 20 '11 at 9:41
add comment

Within the WordPress admin you should be able to order the attached media associated with a post/page/custom once it's been uploaded using the Gallery Tab. This will then effect the order in which they get called from the DB.

share|improve this answer
    
Thanks for your reply. I know this, but I'm wondering how I might hook into this and make use of it for my custom image/attachment handler. Here's a video: youtu.be/zvPnem6ady4 When I've moved the images around, I want the order to be saved. –  INT Jul 16 '11 at 10:10
    
You could set a hidden field up that is updated with jquery then stored as the order. –  Alex Older Jul 18 '11 at 12:49
add comment

How to get post attachments

If you're on a single post view, you'll have the global $post available. If you then want to have all attachments, place the following in your code:

$attachments = array_values( get_children( array( 
     'post_parent'      => $post->post_parent
    ,'post_status'      => 'inherit'
    ,'post_type'        => 'attachment'
    ,'post_mime_type'   => 'image'
    ,'order'            => 'ASC'
    ,'orderby'          => 'menu_order ID' 
) ) );

'menu_order ID' means that you order by 'menu_order' AND 'ID'.

How to sort post attachments

Note: Your attachments are object, close to normal posts. They got an 'ID', got 'menu_order' like pages, etc.

In some special cases, ordering won't work, so you can use a callback function, to trigger ordering by the ID:

usort( $attachments, 'sort_cb' );

Then the function to compare and sort.

function sort( $a, $b )
{
    if ( $a->menu_order == $b->menu_order )
        return null;

    return ( $a->menu_order > $b->menu_order ) ? +1 : -1;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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