Created
January 25, 2013 15:47
-
-
Save acarabott/4635408 to your computer and use it in GitHub Desktop.
Wordpress - Add default menu_order to custom post type, optional terms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add to functions.php | |
// Make sure your custom post type has 'page-attributes' in its supports array | |
// Adjust the 'typeN's to your custom post types | |
// the first type | |
add_filter( 'wp_insert_post_data', 'set_menu_order', 10, 2 ); | |
function set_menu_order( $data, $postarr ) { | |
global $post; | |
$pt = $data['post_type']; | |
if ($pt == 'type1') { | |
$data['menu_order'] = 1; | |
} else if ($pt == 'type2') { | |
// this will check and prioritise type2 custom posts with a particular term | |
if (has_term('term1', 'tax1', $post)) { | |
$data['menu_order'] = 2; | |
} else { | |
$data['menu_order'] = 3; | |
} | |
} else { | |
$data['menu_order'] = 4; | |
} | |
return $data; | |
} | |
// If you need to apply these attributes retroactively use this to update all your posts | |
// Remember to remove it after you have run it once. | |
add_action('init','update_all_products'); | |
function update_all_products(){ | |
$types = array('type1', 'type2'); | |
foreach ($types as $type) { | |
$my_posts = get_posts( array('post_type' => $type, 'numberposts' => -1 ) ); | |
foreach ( $my_posts as $my_post ): | |
wp_update_post( $my_post ); | |
endforeach; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment