Skip Navigation

[Resolved] Imposible to hook on custom post type save or update

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#475914

I am trying to:
Trigger a function when a custom post type (product) is saved or updated (When pressing publish or update button)

I have used the following hooks:

add_action('save_post', 'updated_send_post');

add_action('new_to_publish', 'updated_send_post');
add_action('draft_to_publish', 'updated_send_post');
add_action('pending_to_publish', 'updated_send_post');
add_action('transition_post_status', 'updated_send_post');

add_action('new_to_publish_product', 'updated_send_post');
add_action('draft_to_publish_product', 'updated_send_post');
add_action('pending_to_publish_product', 'updated_send_post');
add_action('transition_post_status_product', 'updated_send_post');

I expected to see:

The function triggered
function updated_send_post(){ echo 'function triggered'; }

Instead, I got:
Nothing, it is only triggered when the custom post type is created, but never more.
The only hook working is 'save_post'

#476275

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Ignacio

I ran a quick test to double-check everything was working and it was (and I'm not sure how it could not be as this is core WordPress functionality, it doesn't have anything particular to do with Toolset).

I added the following code to log when I was saving/updating and publishing a product custom post type:

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

  	$updated = ($update) ? "updated" : "saved";
	error_log("Post type " . $post->post_type . " was " . $updated);
}
add_action( 'save_post', 'test_save_hook', 10, 3 );

function test_draft_hook( $a, $b, $c ) {
	global $post;
	error_log("Post type " . $post->post_type . " was published from draft");
}
add_action( 'draft_to_publish', 'test_draft_hook' );

The first function hooks into save_post (which is triggered whenever a post is saved or updated), and the second into draft_to_publish (which is triggered when a draft post is published).

Here's the debug.log output:

[11-Jan-2017 08:34:21 UTC] Post type product was saved
[11-Jan-2017 08:34:27 UTC] Post type product was updated
[11-Jan-2017 08:34:31 UTC] Post type product was updated
[11-Jan-2017 08:34:39 UTC] Post type product was published from draft
[11-Jan-2017 08:34:39 UTC] Post type product was updated

I did a screen recording where you can see the log being generated, too big to upload here: hidden link

The save_post hook is triggered as soon as I add a new post because of the WordPress auto-draft feature, which updates again before I have chance to save as draft myself, and these account for the first 3 items in the log. Then I publish the draft, which triggers the second hook as well as the first hook again.

All of this was entirely as expected.

As I say, the hooks are a core WordPress component, if they don't appear to be working the most likely explanation is that you have a problem in your code.

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.