Closed saltnpixels closed 8 years ago
Hi @saltnpixels These are WP core functions and I guess out of scope for Pods support, though from looking at the code it seems correct to me. Few checks to be sure:
hey @JoryHogeveen
The hook does fire. if I update a post meta field that was not created in pods it works. And yes a custom post type
Allright. And the "age" field. Is this field also registered with Pods? And if yes, wat kind of field type?
i made it plain but it was a number. Yes it was a pod field. I made ages plural without pods, and that one saves just fine.
the pod fields wont update. Is this just me?
What you can try is set the priority of the action to a higher number (20?). I know Pods also hooks into the save actions so it could be that your data gets overwritten.
it seems this is so. because using wp_die after I set the meta, I see that indeed it is set. So somehow something is unsetting it or reverting it.
I tried setting it to 99... Still doesn't work
and if I use the pods save item hook, I have it looping over and over every time i call update_post_meta lol
Maybe try this? ( save_post
is fired after save_post_{POST_TYPE}
)
function update_profile( $post_id, $post, $update ) {
if ( $post->post_type == 'profile' ) {
update_birthday_age( $post_id );
}
}
add_action( 'save_post', 'update_profile', 20, 3 );
/*--------------------------------------------------------------
# update birthday age from date of birth meta
--------------------------------------------------------------*/
function update_birthday_age( $profile_id ) {
$date_of_birth = get_post_meta( $profile_id, 'birth_date', true );
$from = new DateTime( $date_of_birth );
$to = new DateTime('today');
$birthday = $from->diff( $to )->y;
update_post_meta( $profile_id, 'age', $birthday );
}
no way! why does that work!
Well, Pods hooks into save_post
and updates the metadata from the $_POST variables after sanitizing for all data that is known to Pods.
So this action gets fired later then save_post_{POST_TYPE}
and overwrites your values.
so why does changing the save_post from having the post type change that?
Simply because WP core sets the action with the post_type set before the action without.
So all actions that are hooked into save_post_POSTTYPE
are run before all actions that hook into save_post
.
In your previous code the order would be this:
age
to your value with the save_post_POSTTYPE
hookage
value to the value from the sanitized post data (which is probably empty or something).In the new code both Pods and your code are hooking into save_post
.
Pods is run first (priority 10) and after that your code is run (priority 20).
Ref to the wp_insert_post
function that fires the hooks (line 3386 - 3409:
/**
* Fires once a post has been saved.
*
* The dynamic portion of the hook name, `$post->post_type`, refers to
* the post type slug.
*
* @since 3.7.0
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
/**
* Fires once a post has been saved.
*
* @since 1.5.0
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated or not.
*/
do_action( 'save_post', $post_ID, $post, $update );
See https://developer.wordpress.org/reference/functions/wp_insert_post/
ah I think im understanding. the post_type hook just goes back to the save_post hook which is running before pods somehow? So pods runs and ruins it?
im a front end developer and designer forgive me if Im a bit... er... daft
Its simply an order thing. Pods doesn't "know" that you are setting the value manually and so just looks for the data you send from your browser after hitting save. In your case I think it would be better not to create the 'age' field with Pods since you are handling this data your self.
@JoryHogeveen Thank you so much for your help!
No problem!
@JoryHogeveen Hi! How to be with taxonimies? What hook need to use in case of taxonomies ?
@niocncn
Just google Save taxonomy hook
:
https://www.google.com/search?q=save+taxonomy+hook&oq=save+taxonomy
I made little plugin than use hooks
I want to get model like in REST API, but I get outdated pods params
function getEntity($type,$id)
{
global $wp_rest_server;
$request = new WP_REST_Request( 'GET', '/wp/v2/' . $type . '/' . $id );
$request->set_query_params(['context' => 'edit']);
$request = rest_do_request($request);
return $wp_rest_server->response_to_data($request, true);
}
add_action( 'saved_category', function($id){
$r = getEntity('categories',$id);
var_dump($r); // outdated pods params in array
});
All Standart fields like name, alias and other, updates, but not pods fields. So, i think hook fires before pods updating or some cache clears...
@niocncn Just google
Save taxonomy hook
: https://www.google.com/search?q=save+taxonomy+hook&oq=save+taxonomy
if im trying to use update_post_meta in a save_post hook, nothing happens. It wont save.
Here I have a pod called profile. I am trying to save an age for the profile by getting the birth date of the profile. Both are custom fields made with pods.
This does not work.