0

I would like to change the post_type from "prototyp" to "gutschein" when publishing a "prototyp" (clicking publish button of a "prototyp").

Is there a way to make this automatically by a function?

Edit no.2: Don´t know why, but it still doesn´t seem to work (for me):

function on_post_publish( $ID, $post ) {

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }
if ( get_post_type( $post_id ) == 'prototyp' ) {
    set_post_type( $post_id, 'gutschein' );
    }
}
add_action(  'publish_prototyp',  'on_post_publish', 10, 2 );
3
  • Change add_action( 'publish_post', to add_action( 'publish_prototyp',,
    – cybmeta
    Mar 11, 2015 at 21:28
  • No, that doesn´t work. The post_type stays the same, when publishing a "prototyp".
    – emti
    Mar 11, 2015 at 21:41
  • 1
    It works here, you may need other transition different to "publish". Read the documentation linked in @bueltge's answer.
    – cybmeta
    Mar 11, 2015 at 21:49

1 Answer 1

0

It give a hook to init your function for the requirements. The hook {status}_{post_type} is your right hook. If this hook was fired, you can start your custom function to change the post type. For your custom post type "prototyp" use publish_prototyp.

See also the codex about the hook, also the hook from old status to new status http://codex.wordpress.org/Post_Status_Transitions

For the changing of the post type is the function set_post_type your right choice. See also the codex page for more information. http://codex.wordpress.org/Function_Reference/set_post_type

2
  • Hi, i´ve edited my post and added my first try. But this code seems to do nothing.
    – emti
    Mar 11, 2015 at 21:13
  • You use the wrong hook. If you use the custom post type "prototyp" then is the hook on publish a post of "prototyp" -- publish_prototyp.
    – bueltge
    Mar 11, 2015 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.