WP_Customize_Nav_Menus::save_nav_menus_created_posts()publicWP 4.7.0

Publishes the auto-draft posts that were created for nav menu items.

The post IDs will have been sanitized by already by WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts() to remove any post IDs for which the user cannot publish or for which the post is not an auto-draft.

Method of the class: WP_Customize_Nav_Menus{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Customize_Nav_Menus = new WP_Customize_Nav_Menus();
$WP_Customize_Nav_Menus->save_nav_menus_created_posts( $setting );
$setting(WP_Customize_Setting) (required)
Customizer setting object.

Changelog

Since 4.7.0 Introduced.

WP_Customize_Nav_Menus::save_nav_menus_created_posts() code WP 6.6.2

public function save_nav_menus_created_posts( $setting ) {
	$post_ids = $setting->post_value();
	if ( ! empty( $post_ids ) ) {
		foreach ( $post_ids as $post_id ) {

			// Prevent overriding the status that a user may have prematurely updated the post to.
			$current_status = get_post_status( $post_id );
			if ( 'auto-draft' !== $current_status && 'draft' !== $current_status ) {
				continue;
			}

			$target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
			$args          = array(
				'ID'          => $post_id,
				'post_status' => $target_status,
			);
			$post_name     = get_post_meta( $post_id, '_customize_draft_post_name', true );
			if ( $post_name ) {
				$args['post_name'] = $post_name;
			}

			// Note that wp_publish_post() cannot be used because unique slugs need to be assigned.
			wp_update_post( wp_slash( $args ) );

			delete_post_meta( $post_id, '_customize_draft_post_name' );
		}
	}
}