Permalink
Browse files

Merge pull request #231 from funkedgeek/master

Fix plupload_image field ordering for new uploads
  • Loading branch information...
rilwis committed Feb 28, 2013
2 parents 0bc6499 + c5aebda commit 15856d28be406faa5fec7150c1c6b2b98f018d02
Showing with 22 additions and 6 deletions.
  1. +1 −1 inc/fields/image.php
  2. +21 −5 inc/fields/plupload-image.php
View
@@ -201,4 +201,4 @@ static function meta( $meta, $post_id, $saved, $field )
return (array) $meta;
}
}
-}
+}
@@ -25,6 +25,7 @@ static function add_actions()
*/
static function handle_upload()
{
+ global $wpdb;
$post_id = is_numeric( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : 0;
$field_id = isset( $_REQUEST['field_id'] ) ? $_REQUEST['field_id'] : '';
@@ -33,12 +34,27 @@ static function handle_upload()
// You can use WP's wp_handle_upload() function:
$file = $_FILES['async-upload'];
$file_attr = wp_handle_upload( $file, array( 'test_form' => false ) );
+ //Get next menu_order
+ $meta = get_post_meta( $post_id, $field_id, false );
+ if( empty( $meta ) ){
+ $next = 0;
+ } else {
+ $meta = implode( ',' , (array) $meta );
+ $max = $wpdb->get_var( "
+ SELECT MAX(menu_order) FROM {$wpdb->posts}
+ WHERE post_type = 'attachment'
+ AND ID in ({$meta})
+ " );
+ $next = is_numeric($max) ? (int) $max + 1: 0;
+ }
+
$attachment = array(
- 'guid' => $file_attr['url'],
- 'post_mime_type' => $file_attr['type'],
- 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),
- 'post_content' => '',
- 'post_status' => 'inherit',
+ 'guid' => $file_attr['url'],
+ 'post_mime_type' => $file_attr['type'],
+ 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),
+ 'post_content' => '',
+ 'post_status' => 'inherit',
+ 'menu_order' => $next
);
// Adds file as attachment to WordPress

0 comments on commit 15856d2

Please sign in to comment.