• Resolved popeating

    (@popeating)


    Hello all
    im having a little problem… this is my scenario
    – I have a custom pod type called ‘Course’ with a bunch of custom fields
    – Everytime i save (or publish) a new Course i automatically create a new Woo product based on data i have on the Course post
    – Once the product is created i send back to the post the newly created product and i would like to store in a read only field (so i can later link the product from the Course post)

    It almost works everything correctly, but im not able to update/save the url field at the end of the procedure… my code looks as follow:

    add_action('save_post_course','save_post_callback');
       function save_post_callback($post_id){
       //CODE THAT SAVE THE PRODUCT (THE PRODUCT IS CORRECTLY CREATED)
       //THEN I GRAB THE PERMALINK OF THE JUST CREATED PRODUCT (AND IT WORKS IF I LOG IT)
       $permalink=$permalink=get_permalink( $product_id );
       
       //THEN I TRY TO SAVE THE PERMALINK IN A POD FIELD BUT IT DOESNT SAVE AND GAVE NO ERROR
       $mypod = pods( 'course', $post_id );
       $mypod->save( 'product_url', $permalink);
    
    }

    i tried thousand of variation (passing the data as array, passing th post_id again, passing a fixed string to test, saving in other fields), but no matter how, the permalink is not stored; im pretty sure $mypod is correct, since if i log a display of a field of it it reports the correct value.

    i created hundreds of products with test (in production i would like to check if the product_url is already full to skip the product creation).

    im getting lost and im moving torward making a raw sql query… but i would try to stick to pods functionality API if someone can give me a clue!

    [EDIT]
    it looks like (but im not sure) that when i save a custom post, and i hook in it with
    save_post_course
    the value of the field coming from the page (the submit data) is always having precedence over the $pod->save code part. I noticed if i add a die at the end of the function the save is working, if i remove the ‘die’ and have the page reload (as i want!) the value is overwritten byt the on coming from the post. So im stuck, how can i save the product permalink in the post in a single shot?

    • This topic was modified 3 years, 8 months ago by popeating.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Paul Clark

    (@pdclark)

    It needs to be on the save_post hook with a priority higher than 10, instead of the save_post_{$post_type} hook due to the prioritization of sanitization filters.

    See: https://github.com/pods-framework/pods/issues/3753

    I verified the problem you were having with your code and also verified this code worked:

    add_action(
    	'save_post',
    	function ( $post_id, $post, $update ) {
    		if ( 'course' !== $post->post_type ) {
    			return;
    		}
    		// Replace with your actual $product_id logic.
    		$product_id = 57;
    
    		$mypod = pods( 'course', $post_id );
    		$mypod->save( 'product_url', get_permalink( $product_id ) );
    		
    		// Or:
    		// update_post_meta( $post_id, 'product_url', get_permalink( $product_id ) );
    	},
    	20,
    	3
    );
    
    Thread Starter popeating

    (@popeating)

    Thank you @pdclark that solved the issue in a perfect manner!

    • This reply was modified 3 years, 8 months ago by popeating.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update a pod field on save’ is closed to new replies.