Getting metadata when creating a post in WordPress

I use the save_post action to check the metadata field in a custom column and take some action on this value. These are the main problems as I do this:

add_action('save_post', 'my_save_post'); function my_save_post($post_id) { // Check if not autosaving, processing correct post type etc. // ... // Get the custom field value. $my_field_value = get_post_meta($post_id, 'my_field', true); // Do some action // ... } 

This works great when updating a message through the admin page. However, the first time a message is my_field_value always empty. The field is indeed saved correctly, but this action trigger does not seem to be able to see it, as well as no other values ​​for the custom field.

I would like the action to be performed on all posts of this type, and I will import a lot through CSV Imported. Even then, custom fields are imported correctly, and the action trigger is fired for each imported row, but the save_post action still cannot see the value of the custom field.

As far as I can see from the documentation, a message has already been created by the time this action is run, so I should always see this custom metafield.


The answer seems to be in the order in which everything happens. When creating a message from a form, user fields are collected by appropriate actions and added to the message before the save_post action begins. This means that my trigger is able to see these custom field values.

When importing from CSV, primary mail is created first, and then custom metafiles are added. The save_post trigger is triggered the first time it is created, before the metafiles are added, and therefore user field data is not visible to the save_post action.

My solution was to catch metadata updates using the updated_post_meta and added_post_meta , as well as the save_post actions:

 add_action('updated_post_meta', 'my_updated_post_meta', 10, 4); add_action('added_post_meta', 'my_updated_post_meta', 10, 4); function my_updated_post_meta($meta_id, $post_id, $meta_key, $meta_value) { // Make sure we are handling just the meta field we are interested in. if ($meta_key != 'my_custom_field') return; if (wp_is_post_revision($post_id)) return; if (get_post_type($post_id) != 'my_post_type') return; if (trim($meta_value) == '') return; // Do my custom task (linking this post to a parent post in a different // post type). This is the same task performed by the save_post action. my_link_product_track($post_id, trim($meta_value)); } 

This is essentially what I am doing and it seems to work well. I encapsulate all of the above in a custom class in the subject and do not recommend using global scope variables, as shown here, but this is only to show the method.

+4
source share
1 answer

You should use $post->ID instead of $post_id -

 $my_field_value = get_post_meta($post->ID, 'my_field', true); 

get_post_meta in code

EDIT

Could you do something like this?

 if($post->ID == ''){ $pid = $post_id; } else { $pid = $post->ID; } //$pid = $post->ID or $post_id, whichever contains a value $my_field_value = get_post_meta($pid, 'my_field', true); 

is there something that looks for the value in $ post-> ID and $ post_id, and uses one that is not empty?

+2
source

Source: https://habr.com/ru/post/1438770/


All Articles