wp_publish_post() WP 2.1.0

Публікує запис. Змінює її статус з future , draft і т.д. на publish . Запускає всі хуки, необхідні для зміни статусу запису.

Докладніше читайте в описі функції: wp_transition_post_status()

Якщо вам потрібно змінити статус запису, наприклад з draftна publish, то краще запустити функцію wp_update_post() :

wp_update_post([
	'ID' => $post_id,
	'post_status' => 'publish',
]);

Ця функція не робить всіх необхідних операцій для правильної зміни статусу, зокрема не виставляються дати посту і можливо ще щось.

Працює на основі:
wp_transition_post_status()

Хуки з функції

Повертає

null. Нічого (null)

Використання

wp_publish_post($post_id);
$post_id
(число/WP_Post) (обов’язковий)
Об’єкт або ID запису.

Приклади

0

#1 Публікація посту із затримкою

Допустимо нам потрібно опублікувати пост лише якщо пройдено якусь перевірку. Для цього ми додаємо пост до БД зі статусом pending. Потім робимо нашу перевірку і якщо її пройдено публікуємо пост, а якщо не пройдено, то нічого не робимо.

function add_coin_post(){ // Додаємо пост до БД $post_data = [ 'post_type' => 'coin', 'post_title' => $args['name'], 'post_name' => sanitize_title( $symbol ), 'post_status' => 'pending', //! IMPORTANT ]; $post_id = wp_insert_post( wp_slash( $post_data ), true, false ); if( is_wp_error( $post_id ) ){ return $post_id; } // РОБИМО ПОТРІБНУ НАМ ПЕРЕВІРКУ // де буде визначено змінну $check_is_ok, якщо все ок if( $check_is_ok ){ // запускаємо хуки публікації на яких будуть зроблені // Операції для нового опублікованого посту wp_publish_post($post_id); } }

нотатки

  • Global. wpdb. $wpdb WordPress database abstraction object.

список змін

З версії 2.1.0Введено.

Код wp_publish_post() WP 6.0.2

function wp_publish_post( $post ) { Global $wpdb; $post = get_post($post); if (! $post) { return; } if ( 'publish' === $post->post_status ) { return; } $post_before = get_post( $post->ID ); // Ensure at least one term is applied for taxonomies with default term. foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) { // Skip taxonomy if no default term is set. if ( 'category' !== $taxonomy && empty( $tax_object->default_term ) ) { continue; } // Do not modify previously set terms. if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) { continue; } if ( 'category' === $taxonomy ) { $default_term_id = (int) get_option( 'default_category', 0); } else { $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 ); } if ( ! $default_term_id ) { continue; } wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy ); } $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); clean_post_cache( $post->ID ); $old_status = $post->post_status; $post->post_status = 'publish'; wp_transition_post_status( 'publish', $old_status, $post ); /** Ця дія міститься в wp-includes/post.php */ do_action( "edit_post_{$post->post_type}", $post->ID, $post ); /** Ця дія міститься в wp-includes/post.php */ do_action( 'edit_post', $post->ID, $post ); /** Ця дія міститься в wp-includes/post.php */ do_action( "save_post_{$post->post_type}", $post->ID, $post, true); /** Ця дія міститься в wp-includes/post.php */ do_action( 'save_post', $post->ID, $post, true); /** Ця дія міститься в wp-includes/post.php */ do_action( 'wp_insert_post', $post->ID, $post, true); wp_after_insert_post($post, true, $post_before); }

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *