Ecommerce Shopify WordPress Discussion

WordPress: Issue with Custom Meta Field Not Updating with Current Custom Post Meta Values on Post Edit

I'm encountering an issue with a custom post type ('sesiones') in WordPress, where one of the custom post meta fields ('enlace_calendar') is not updating correctly when the post is edited. Here's a breakdown of the problem: Background: I've set up a custom post type called 'sesiones' using the JetEngine plugin. This post type includes several custom post meta fields, such as 'sesion-description', 'sesion-hour-start', 'sesion-hour-end', and 'location'. Objective: The goal is to auto-populate the 'enlace_calendar' meta field each time the post is edited. This meta field should use the current values of the other custom meta fields within the same post to generate its own value. Issue: However, upon hitting the 'update' button in the WordPress editor, the 'enlace_calendar' meta field is not updating with the latest values of the other meta fields. Instead, it retains the values that were saved before the edit. The function responsible for populating the 'enlace_calendar' meta field seems to be using outdated values of the other meta fields, rather than the current ones. I believe this has something to do with how the custom meta values are being stored and the order in which WP receives this information...but I don't know how to fix it This is the function that is running everytime I update the post. function calendar_post_edit( $post_id ) { $post = get_post($post_id); if($post->post_type == 'sesiones'){ $enlace_base = ''; $description = get_post_meta($post_id, 'sesion-description', true); $inicio = utc_convert(get_post_meta($post_id, 'sesion-hour-start', true), "Ymd\THis\Z"); $fin = utc_convert(get_post_meta($post_id, 'sesion-hour-end', true), "Ymd\THis\Z"); $location = get_post_meta($post_id, 'location', true); $parametros = [ 'text' => $post->post_title, 'location' => $location, 'details' => $description, 'dates' => $inicio . '/' . $fin, ]; $enlace_completo = $enlace_base . '&' . http_build_query($parametros); update_post_meta( $post_id, 'enlace_calendar', $enlace_completo); } } add_action( 'edit_post', 'calendar_post_edit'); Let's say I created the post with 'sesion-hour-start' = 19-feb-2024 13:00. If I save this post, 'enlace_calendar' would only have the 'text' value correct, which is the post title. If I go back into the same post again and hit the "update" button (without doing any changes) 'enlace_calendar' will have the correct values. If I go back into the post and change the value of any meta, let's say 'sesion-hour-start' = 20-feb-2024 13:00 and hit 'update', 'enlace_calendar' will still have the old data (19-feb-2024) I will have to hit 'update' again for the correct value to show. Any insights or suggestions on how to ensure that the 'enlace_calendar' meta field updates with the current values of other meta fields whenever a post in the 'sesiones' post type is edited would be greatly appreciated.
I was using the wrong hook. I should have used 'updated_post_meta' since my problem was that the post metadata was not being reached correctly. This is my final code. Hope this helps someone function calendar_post_edit( $meta_id, $object_id, $meta_key, $meta_value ) { // Check if the updated meta key is for the sesiones post type $post_type = get_post_type( $object_id ); if ( 'sesiones' === $post_type ) { // Check if the updated meta key is one of the sesiones custom fields $sesiones_meta_keys = array( 'sesion-description', 'sesion-hour-start', 'sesion-hour-end', 'location' ); if ( in_array( $meta_key, $sesiones_meta_keys ) ) { // Perform actions to update enlace_calendar meta $enlace_base = ''; $description = get_post_meta( $object_id, 'sesion-description', true ); $inicio = utc_convert( get_post_meta( $object_id, 'sesion-hour-start', true ), "Ymd\THis\Z" ); $fin = utc_convert( get_post_meta( $object_id, 'sesion-hour-end', true ), "Ymd\THis\Z" ); $location = get_post_meta( $object_id, 'location', true ); $parametros = array( 'text' => get_the_title( $object_id ), 'location' => $location, 'details' => $description, 'dates' => $inicio . '/' . $fin, ); $enlace_completo = $enlace_base . '&' . http_build_query( $parametros ); update_post_meta( $object_id, 'enlace_calendar', $enlace_completo ); } } } add_action( 'updated_post_meta', 'calendar_post_edit', 10, 4 );

February 26, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.