admin管理员组文章数量:1122832
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?
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 edited Sep 29, 2010 at 1:00 MikeSchinkel 37.5k14 gold badges116 silver badges132 bronze badges asked Sep 29, 2010 at 0:41 fxfuturefxfuture 1,3374 gold badges24 silver badges40 bronze badges3 Answers
Reset to default 7Hi @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;
}
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
Updated the answer as the post_status draft gave a notice. Instead, I'm checking for a zero (unchanged) menu order and giving it a unique menu order (highest in database + 5). Adjust to your needs.
add_filter( 'wp_insert_post_data', 'mp_wp_insert_post_data' );
function mp_wp_insert_post_data( $data ) {
$post_type = 'page';
if ( $post_type === $data['post_type'] && 0 === $data['menu_order'] ) {
global $wpdb;
$data['menu_order'] = $wpdb->get_var( "SELECT MAX(menu_order)+5 AS menu_order FROM {$wpdb->posts} WHERE post_type='{$post_type}'" );
}
return $data;
}
本文标签: Define menuorder on Creation of New Custom Post
版权声明:本文标题:Define menu_order on Creation of New Custom Post? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301791a1931300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论