WordPress.org

Ready to get started?Download WordPress

Forums

Add default menu_order value (3 posts)

  1. fabianocoelho
    Member
    Posted 3 years ago #

    Hi
    I want to be able to add a menu_order value=1 to every custom post_type I create, is it possible? Don't know, by creating a filter or something like that…

    I have a post_type = products, I want to set menu_order = 1 to all new posts under this post_type

  2. Are you manually creating the custom post types in the functions file, or using a plugin to create them? Either way, you should be able to set the menu_position parameter described in detail on the register_post_type Codex page.

  3. Here is a filter I wrote to set the menu order on new posts. This sets it to 8 but you can change where it states +8 to +1.

    `add_filter( 'wp_insert_post_data', 'my_wp_insert_post_data', 10, 2 );
    function my_wp_insert_post_data( $data, $postarr ) {
    global $post;
    $post_type= get_post_type( $post );
    if ( $data[ 'post_type' ] == $post_type && get_post( $postarr[ 'ID' ] )->post_status == 'draft' ) {
    global $wpdb;
    $data[ 'menu_order' ]=$wpdb->get_var( "SELECT MIN(menu_order)+8 AS menu_order FROM {$wpdb->posts} WHERE post_type='{$post_type}'" );
    }
    return $data;
    }`

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.