• Hi,

    I want to sync values between two posts so that when one of them changes ( “the parent” ) it updates other post automatically.
    I tried ‘edit_post’, ‘post_updated’ and ‘save_post_{$post->post_type}’.
    All of them work when I quick edit as administrator, and save_post works also when I create a post anywhere on my site. But what I need is for it to work everytime a any of the post variables get updated.

    This is how I am using it:

    
    add_action( 'save_post_product', 'sync_producto_contenido', 10, 3);
    function sync_producto_contenido( int $post_ID, WP_Post $post, bool $update)
    {
    	deb("Sincronizando producto y contenido..");
    	// Campos del producto que hemos creado
    	$producto_title = get_the_title( $post_ID);
    ..
    ..
    }

    Any idea?
    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    When using the block editor, saves are performed through the REST API. Depending on what has changed, “save_post_{$post_type}” action nay not fire. A different action that specifically targets the change may be more appropriate. You do not indicate what change you are trying to capture nor how you attempt to sync it, so it’s difficult to advise further.

    Thread Starter pablomon

    (@pablomon)

    Hi @bcworkz,

    Thanks for your answer. Let me elaborate further.

    When the user creates a “course” post on the frond end I want to be able to capture that information and use it to create the “lessons” automatically. The lessons would inherit the title, content, featured image, a taxonomy and some metas.
    Likewise if the users edit a course, I want to transmit those changes to the children posts I automatically.

    My idea was to listen for any change on the specific post type ( its a product post type ) and then check wether its being updated or created hooking to save_post_{$post_type}. But this could just be a completely wrong approach?

    I am totally new to wordpress so please forgive the terminology I am using.

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    Terminology is excellent. I wish I were as on top of things when I started out ??

    The action “save_post_{$post_type}” should fire when saving or updating from the block editor. There are numerous WP editors though. Add on editors like Elementor could behave differently. I just double checked for one of my own post types to be sure nothing has recently changed.

    Could there be something about your callback that isn’t working which makes you think it has failed to run, but in fact the action did fire? That hook should suffice for data that’s within the passed post object like title and content. Other data like meta data and assigned taxonomy terms is not as readily available, especially when only those items are updated and the post object itself remains unchanged.

    Getting freshly written data from the DB when “save_post_{$post_type}” fires is unreliable at best. Typically a race condition is encountered where your callback executes before the DB engine has written the new data to the DB. Added to that is when such ancillary data is updated through the API, “save_post_{$post_type}” may not fire at all. Thus for such changes you should use a hook specific for the data being saved, such as “update_postmeta” or “add_term_relationship”. Then the data you need is passed to your callback and you don’t need to get it through less reliable means.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Save_post hook only working on quickedit’ is closed to new replies.