最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post type editpublish hook

programmeradmin2浏览0评论

What hook gets called when I edit or publish a custom post type of name 'episode gallery' ?

I tried following but none worked.

add_action('save_post', 'update_custom_ngg_table');
add_action('update_postmeta', 'update_custom_ngg_table');
add_action('publish_post', 'update_custom_ngg_table');
add_action('edit_post', 'update_custom_ngg_table');
add_action('edit_page', 'update_custom_ngg_table');
add_action('publish_page', 'update_custom_ngg_table');
add_action('save_page', 'update_custom_ngg_table');
add_action('publish_episode-gallery', 'update_custom_ngg_table');

I am about to give up :)

What hook gets called when I edit or publish a custom post type of name 'episode gallery' ?

I tried following but none worked.

add_action('save_post', 'update_custom_ngg_table');
add_action('update_postmeta', 'update_custom_ngg_table');
add_action('publish_post', 'update_custom_ngg_table');
add_action('edit_post', 'update_custom_ngg_table');
add_action('edit_page', 'update_custom_ngg_table');
add_action('publish_page', 'update_custom_ngg_table');
add_action('save_page', 'update_custom_ngg_table');
add_action('publish_episode-gallery', 'update_custom_ngg_table');

I am about to give up :)

Share Improve this question asked Dec 20, 2010 at 19:01 vickvick
Add a comment  | 

4 Answers 4

Reset to default 7

Unless I misunderstand your question, you want {$new_status}_{my-custom-post-type}

Take a look at the hook registration.

This page (from Pippin's Posts)* does a better job than I could at explaining it, but from your example, you would want add_action('publish_episode-gallery', 'update_custom_ngg_table');

According to the source, the following status are available: publish, future, draft, pending, private, trash, auto-draft and inherit.

* Mirror at the Web Archive

It's always save_post, no matter what the post type. save_post also runs when creating and publishing a new post.

From inside your hooked function you can figure out what kind of post is being handled--it gets passed both the post id and the entire post object.

function wpsx_5688_update_post($post_id, $post) {

    // Make sure the post obj is present and complete. If not, bail.
    if(!is_object($post) || !isset($post->post_type)) {
        return;
    }

    switch($post->post_type) { // Do different things based on the post type

        case "episode-gallery":
            // Do your episode gallery stuff
            break;

        case "another-post-type-slug":
            // Do other stuff
            break;

        default:
            // Do other stuff

    }

}
add_action('save_post', 'wpsx_5688_update_post', 1, 2);

Another thing to consider is that--depending on your exact problem, I'm not sure what you're trying to do--it might not be that the hook isn't firing, but that the code inside your hooked function has errors. Just something else to check if you're stuck.

If you want to perform an action when any custom post Edit/insert-

add_action('save_post','save_post_callback');
function save_post_callback($post_id){
global $post; 
if ($post->post_type != 'MY_CUSTOM_POST_TYPE_NAME'){
    return;
}
//if you get here then it's your post type so do your things....
}

If you want to make a change to a specific post type, you can also use the save_post_$this->post_type hook documented here.

$post_type = "enter_your_post_type_name_here";
add_action( 'save_post_' . $post_type, function( $post_id, $post, $update ) {
    // Anything inside this function will run when the specified post type is updated.
} );

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论