pods-framework / pods

The Pods Framework is a Content Development Framework for WordPress - It lets you create and extend content types that can be used for any project. Add fields of various types we've built in, or add your own with custom inputs, you have total control.
https://pods.io/
GNU General Public License v2.0
1.07k stars 264 forks source link

Save_post hook cannot use update_post_meta #3753

Closed saltnpixels closed 8 years ago

saltnpixels commented 8 years ago

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.

function update_profile( $post_id, $post, $update ) {

  update_birthday_age($post_id); 

}
add_action( 'save_post_profile', 'update_profile', 10, 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);

 }

This does not work.

JoryHogeveen commented 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:

saltnpixels commented 8 years ago

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

JoryHogeveen commented 8 years ago

Allright. And the "age" field. Is this field also registered with Pods? And if yes, wat kind of field type?

saltnpixels commented 8 years ago

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?

JoryHogeveen commented 8 years ago

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.

saltnpixels commented 8 years ago

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

saltnpixels commented 8 years ago

and if I use the pods save item hook, I have it looping over and over every time i call update_post_meta lol

JoryHogeveen commented 8 years ago

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 );
 }
saltnpixels commented 8 years ago

no way! why does that work!

JoryHogeveen commented 8 years ago

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.

saltnpixels commented 8 years ago

so why does changing the save_post from having the post type change that?

JoryHogeveen commented 8 years ago

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:

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/

saltnpixels commented 8 years ago

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

JoryHogeveen commented 8 years ago

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.

saltnpixels commented 8 years ago

@JoryHogeveen Thank you so much for your help!

JoryHogeveen commented 8 years ago

No problem!

niocncn commented 2 years ago

@JoryHogeveen Hi! How to be with taxonimies? What hook need to use in case of taxonomies ?

JoryHogeveen commented 2 years ago

@niocncn Just google Save taxonomy hook: https://www.google.com/search?q=save+taxonomy+hook&oq=save+taxonomy

niocncn commented 2 years ago

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