Reputation: 177
I would like to create a Woocommerce Product whenever a post of post type "award_category" is either added or updated.
Award Categories contain a number of ACF fields and I would like to carry some of this data from an award category post to a Product post.
This is the code I currently have which runs every time an award category is saved:
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');
The problem I am having is that the Post Description I'm pulling through is either empty (when you first add an award category) or it pulls through the former product description (before it was saved).
I am absolutely convinced I am using the wrong Wordpress hook as the award category post is being saved AFTER I try and grab the ACF fields from it.
I hope I've explained myself well enough and hope you can offer some assistance!
Upvotes: 0
Views: 2358
Reputation: 11282
You can use wp_after_insert_post
hook that recently added in version 5.6.0
/**
* 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 );
Or you can use updated_post_meta
hook.
/**
* 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 );
Upvotes: 3