On this page
_wp_auto_add_pages_to_menu( string $new_status, string $old_status, WP_Post $post )
This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Automatically add newly published page objects to menus with that as an option.
Parameters
$new_statusstring Required-
The new status of the post object.
$old_statusstring Required-
The old status of the post object.
$postWP_Post Required-
The post object being transitioned from one status to another.
Source
File: wp-includes/nav-menu.php. View all references
function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) {
return;
}
if ( ! empty( $post->post_parent ) ) {
return;
}
$auto_add = get_option( 'nav_menu_options' );
if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) {
return;
}
$auto_add = $auto_add['auto_add'];
if ( empty( $auto_add ) || ! is_array( $auto_add ) ) {
return;
}
$args = array(
'menu-item-object-id' => $post->ID,
'menu-item-object' => $post->post_type,
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish',
);
foreach ( $auto_add as $menu_id ) {
$items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
if ( ! is_array( $items ) ) {
continue;
}
foreach ( $items as $item ) {
if ( $post->ID == $item->object_id ) {
continue 2;
}
}
wp_update_nav_menu_item( $menu_id, 0, $args );
}
}
Related
Uses
| Uses | Description |
|---|---|
| wp_update_nav_menu_item() wp-includes/nav-menu.php | Saves the properties of a menu item or create a new one. |
| wp_get_nav_menu_items() wp-includes/nav-menu.php | Retrieves all menu items of a navigation menu. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/_wp_auto_add_pages_to_menu