• Resolved yarnboy

    (@yarnboy)


    Hello everyone,

    I’m trying to add custom post meta for a custom post type on publish (or on update if the post meta doesn’t already exist). I’ve done some research, tried a bunch of things, but nothing has worked so far. The custom post type is “portfolio.” Here is my code:

    function add_portfolio_position($post) {
    	$post_id = $post->ID;
    
    	add_post_meta( $post_id, '_portfolio_position', 0, true);
    }
    
    add_action('new_to_publish_portfolio', 'add_portfolio_position');
    add_action('draft_to_publish_portfolio', 'add_portfolio_position');
    add_action('pending_to_publish_portfolio', 'add_portfolio_position');

    Any and all suggestions appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The {old_status}_to_{new_status} action hooks do not allow you to specify post_type.

    You can add a post_type argument to: ‘publish_portfolio’

    function add_portfolio_position( $post_id ) {
        add_post_meta( $post_id, '_portfolio_position', 0, true);
    }
    add_action( 'publish_portfolio', 'add_portfolio_position' );

    See wp_transition_post_status() function definition for more info.

    Thread Starter yarnboy

    (@yarnboy)

    Thanks Chris! Worked brilliantly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add post meta for custom post type on publish’ is closed to new replies.