= 3.8) { $title = '' . __('Edit Content', 'bs_abep') . ''; $img = ''; } else { $title = '' . __('Edit Content', 'bs_abep') . ''; $img = '_no_dashicon'; } $admin_url = admin_url(); // Add the Parent link. $wp_admin_bar->add_menu( array( 'title' => $title, 'href' => false, 'id' => 'bs_abep_links'.$img, )); $args = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_status' => 'publish', 'posts_per_page' => -1, ); foreach ($options as $post_type => $nice_name){ $args['post_type'] = $post_type; // Filter the args now if (has_filter('bs_abep_query_args')) { $args = apply_filters('bs_abep_query_args', $args); // Let's reset the post type in case the user inadvertently changed it - there's really no reaon to change it. $args['post_type'] = $post_type; } $bs_abep_query = new WP_Query( $args ); if ( $bs_abep_query->have_posts() ) : // We have some posts, let's add a parent menu $wp_admin_bar->add_menu( array( 'title' => $nice_name, 'href' => $admin_url . 'edit.php?post_type=' . $post_type, 'id' => $post_type, 'parent' => 'bs_abep_links'.$img )); if ( is_post_type_hierarchical($post_type) && 'menu_order' == $args['orderby'] ) { // Sort them into a hierarchical list here $bs_abep_query->posts = bs_abep_sort_hierarchical_posts( $bs_abep_query->posts ); } foreach ($bs_abep_query->posts as $post) { if ( 0 !== $post->post_parent && 'menu_order' == $args['orderby'] ) { $label = '  – '.ucwords($post->post_title); $parent_id = $post->post_parent; // Loop through to indent the post type the appropriate number of times for ( $i = 0; $i < count( get_post_ancestors($parent_id) ); $i++ ) { $label = '   '.$label; } } else { $label = ucwords($post->post_title); } $url = get_edit_post_link($post->ID); $wp_admin_bar->add_menu( array( 'title' => $label, 'href' => $url, 'id' => $post->ID, 'parent' => $post_type )); } endif; } } function bs_abep_sort_hierarchical_posts( $posts ) { $final_post_array = array(); foreach ( $posts as $index => $post ) { if ( 0 == $post->post_parent ) { $post_id_array[0][$index] = $post->ID; } else { $post_id_array[$post->post_parent][$index] = $post->ID; } } ksort($post_id_array); foreach ( $post_id_array[0] as $index => $post_id ) { $final_post_array[] = $posts[$index]; bs_abep_check_children($post_id, $post_id_array, $final_post_array, $posts); } return $final_post_array; } function bs_abep_check_children( $post_id, $post_id_array, &$final_post_array, $posts ) { if ( isset( $post_id_array[$post_id] ) ) { foreach( $post_id_array[$post_id] as $index => $child_id ) { $final_post_array[] = $posts[$index]; bs_abep_check_children($child_id, $post_id_array, $final_post_array, $posts); } } } function bs_abep_activation_callback() { $options = get_option('bs_abep_settings', array()); $default = array( 'types' => array( 'page' => 'Pages', ), ); if ( empty($options) ) { update_option('bs_abep_settings', $default ); } }