Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have more than a hunderd posts from custom post type and I have to add custom fields to each post, I've used 'save_post' to trigger update/publish event, but for me it works only when "Publish" button is pressed on the edit page.

I want to use "Bulk Edit" to update old posts, but 'save_post' hook doesn't work for me.

Here's my code:

function wpse_75167_add_custom_meta( $post_id ) {
    global $ptypes;

    $cptype = get_post_type();

    // Add Custom Meta to Specific Posts Only
    if( !array_key_exists( $cptype, $ptypes ) ) 
        return;

    // Get Parent Venue ID
    $parent_id = get_post_meta( $post_id, $ptypes[$cptype]['meta_key_parent'], true );

    // Get Venue Title
    if( ! $parent_id ) 
        return;

    $post_title = get_the_title( $parent_id );

    // Check If Meta Key Exists
    if( get_post_meta( $post_id, $ptypes[$cptype]['meta_key_sortable'], true ) ) {
        update_post_meta( $post_id, $ptypes[$cptype]['meta_key_sortable'], $post_title );
    } else {
        add_post_meta( $post_id, $ptypes[$cptype]['meta_key_sortable'], $post_title );
    }
}
add_action( 'save_post', 'wpse_75167_add_custom_meta' );
share|improve this question
1  
Is the ptypes global defined by you? :::: Your code has to be $cptype = get_post_type($post_id);; :::: The save_post action is triggered when bulk-editing. – brasofilo Dec 6 '12 at 17:21
Thanks a lot! I've changed it and everything works well now! – Kristian Vitozev Dec 6 '12 at 18:47

closed as too localized by brasofilo, toscho Dec 6 '12 at 19:02

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.