WordPress publish_post hook not firing for custom post type
10,049
Solution 1
The 'publish_post' action is post type specific. So if you have a custom post type, you need to change the hook you use. if your post type is job_listing
, the hook you should use is publish_job_listing
.
function newJobAdded($ID, $post ) {
mail('[email protected]','new job published','done new job publish');
}
add_action( 'publish_job_listing', 'newJobAdded', 10, 2 );
Solution 2
Try with
function newJobAdded($ID, $post) {
}
instead of
function newJobAdded() {
}
Reference: publish_post
Comments
-
MarkP almost 2 years
I'm currently doing some work with the WP Job Board Manager plugin and I'm wanting to create a function that will fire when a new job is published.
The first thing I did was to create the general hook to find out what the post type was:
function newJobAdded() { $posttype = get_post_type( $post ); mail('[email protected]','new job published',$posttype); } add_action( 'publish_post', 'newJobAdded' );
Which sent me an email telling me that the post type was: job_listing. I then created a new function that would only fire if the custom post type was job_listing
function newJobAdded() { $posttype = "job_listing"; if ($post->post_type == $posttype) { mail('[email protected]','new job published','done new job publish'); } } add_action( 'publish_post', 'newJobAdded' );
However, nothing happens when I do this. Am I missing something simplistic and noobish?
-
MarkP over 9 yearsCan you tell me what the '10, 2' is for?
-
Vidya L over 9 yearsthat '10, 2' is for priority and number of arguments respectively, check codex.wordpress.org/Function_Reference/add_action
-
MarkP over 9 yearsThis gives: Parse error: syntax error, unexpected T_FUNCTION
-
MarkP over 9 yearsHey, this works great. It does however also fire when the user saves a job_listing already there. Can I change this to only fire upon initial publish? Much appreciated.
-
Pieter Goosen over 9 yearsThen you are using a PHP version older than 5.3. See my update
-
Vidya L over 9 yearstry adding a conditional statement like if( ( $post['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) )