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.

Метод класса: WP_Customize_Nav_Menus{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WP_Customize_Nav_Menus = new WP_Customize_Nav_Menus();
$WP_Customize_Nav_Menus->save_nav_menus_created_posts( $setting );
$setting(WP_Customize_Setting) (обязательный)
Customizer setting object.

Список изменений

С версии 4.7.0 Введена.

Код WP_Customize_Nav_Menus::save_nav_menus_created_posts() WP 6.4.3

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' );
		}
	}
}