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 created a custom post type called video and I'm using a plugin called Playlist_Order (which changes the menu_order field) to allow the user to use a drag and drop interface to order their videos in a playlist.

However, when adding a new post the video appears at the top of the playlist because it's given a default menu_order value of 0.

On creation of a new video post I would like it to appear last in the playlist - i.e. query all video post types, find the largest menu_order value and then set this +1 for the new post.

How can I implement this?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Hi @fxfuture:

I think what you are looking for is the wp_insert_post_data hook. You can add this code to the bottom of your theme's functions.php file and/or you can add it to a plugin:

add_filter('wp_insert_post_data','my_wp_insert_post_data',10,2);
function my_wp_insert_post_data($data, $postarr) {
  $post_type = 'video';
  if ($data['post_type']==$post_type && get_post($postarr['ID'])->post_status=='draft') {
    global $wpdb;
    $data['menu_order'] = $wpdb->get_var("SELECT MAX(menu_order)+1 AS menu_order FROM {$wpdb->posts} WHERE post_type='{$post_type}'");
  }
  return $data;
}
share|improve this answer
    
Hi Mike - thanks very much! This works great! Not that it really matters too much but for some reason if I only fill out the title and leave the custom meta fields blank the function doesn't work? –  fxfuture Sep 29 '10 at 9:36
    
It may be that post_status isn't always 'draft'; not sure. Is it safe to assume your numbering can start with 1 or do you need to start numbering with 0? If the former we can change it to avoid any potential mishap with post_status. –  MikeSchinkel Sep 30 '10 at 5:39
add comment

the 10 & 2 arguments in the end of the add_filter method are: the priority of the execution (optional default=10) and number of arguments the new function receives (optional default=1)

the only reason it appears in the code above it's to change the default number of argument.

read more here http://codex.wordpress.org/Function_Reference/add_filter

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.