簡體   English   中英

Wordpress - 添加或保存帖子后運行的功能

[英]Wordpress - Function to Run After Post is Added or Saved

每當添加或更新帖子類型為“award_category”的帖子時,我都想創建一個 Woocommerce 產品。

獎項類別包含許多 ACF 字段,我想將其中一些數據從獎項類別帖子傳送到產品帖子。

這是我目前每次保存獎勵類別時運行的代碼:

  • 查看是否已經有同名產品
  • 如果沒有,請創建一個新的
  • 如果它已經創建了一個新的或現有的,請更新此帖子的名為“描述”的 ACF 字段

function save_award_category($post_id) {

    // Get Post Object

    $post = get_post($post_id);

    // Check if There is a Product with the Same Name

    $product = get_page_by_path($post->post_name, OBJECT, 'product');       

    // If Product With Same Name Doesn't Exist

    if (!$product) {        

        // Create Product Post

        $product_id = wp_insert_post(array(
            'post_title' => get_the_title($post->ID),
            'post_type' => 'product',
            'post_status' => 'publish'
        ));                     

    }

    // Get Post ACF Field Group Array

    $post = get_field('all_fields', $post->ID);

    // Update Product Description with Post Description

    update_field('description', $post['description'], $product_id);

}

add_action('save_post_award_category', 'save_award_category');

我遇到的問題是我正在瀏覽的帖子描述要么是空的(當您第一次添加獎勵類別時),要么它瀏覽了以前的產品描述(在保存之前)。

我絕對相信我使用了錯誤的 Wordpress 鈎子,因為在我嘗試從中獲取 ACF 字段后,獎勵類別帖子正在被保存。

我希望我已經很好地解釋了自己,希望你能提供一些幫助!

您可以使用最近在5.6.0版中添加的wp_after_insert_post鈎子

/**
 * Fires once a post, its terms and meta data has been saved.
 *
 * @since 5.6.0
 *
 * @param int          $post_id     Post ID.
 * @param WP_Post      $post        Post object.
 * @param bool         $update      Whether this is an existing post being updated.
 * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
 *                                  to the update for updated posts.
 */

function save_award_category( $post_id, $post, $update, $post_before ) {

    if ( get_post_type( $post_id ) == 'award_category' ) {

        // Get Post Object

        $post = get_post( $post_id );

        // Check if There is a Product with the Same Name

        $product = get_page_by_path( $post->post_name, OBJECT, 'product' );       

        // If Product With Same Name Doesn't Exist

        if ( !$product ) {

            // Create Product Post

            $product_id = wp_insert_post(array(
                'post_title'  => get_the_title($post->ID),
                'post_type'   => 'product',
                'post_status' => 'publish'
            ));                     

        }

        // Get Post ACF Field Group Array

        $post = get_field( 'all_fields', $post->ID );

        // Update Product Description with Post Description

        update_field( 'description', $post['description'], $product_id );

    }
            
}

add_action( 'wp_after_insert_post', 'save_award_category', 10, 4 );

或者你可以使用updated_post_meta鈎子。

/**
 * Fires immediately after updating metadata of a specific type.
 *
 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
 * (post, comment, term, user, or any other type with an associated meta table).
 *
 * Possible hook names include:
 *
 *  - `updated_post_meta`
 *  - `updated_comment_meta`
 *  - `updated_term_meta`
 *  - `updated_user_meta`
 *
 * @since 2.9.0
 *
 * @param int    $meta_id     ID of updated metadata entry.
 * @param int    $object_id   ID of the object metadata is for.
 * @param string $meta_key    Metadata key.
 * @param mixed  $_meta_value Metadata value. Serialized if non-scalar.
 */

function save_award_category( $meta_id, $post_id, $meta_key = '', $meta_value = '' ) {

    if ( get_post_type( $post_id ) == 'award_category' ) {

        // Get Post Object

        $post = get_post( $post_id );

        // Check if There is a Product with the Same Name

        $product = get_page_by_path( $post->post_name, OBJECT, 'product' );       

        // If Product With Same Name Doesn't Exist

        if ( !$product ) {

            // Create Product Post

            $product_id = wp_insert_post(array(
                'post_title'  => get_the_title($post->ID),
                'post_type'   => 'product',
                'post_status' => 'publish'
            ));                     

        }

        // Get Post ACF Field Group Array

        $post = get_field( 'all_fields', $post->ID );

        // Update Product Description with Post Description

        update_field( 'description', $post['description'], $product_id );

    }
            
}

add_action( 'wp_after_insert_post', 'save_award_category', 10, 4 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM