admin管理员组

文章数量:1326447

I was using set_post_type( $post_id, 'post' ); to create the post in type post from a form. But now I want to submit the form in two post types- post and asd.

I was using set_post_type( $post_id, 'post' ); to create the post in type post from a form. But now I want to submit the form in two post types- post and asd.

Share Improve this question edited Aug 4, 2020 at 15:46 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Aug 4, 2020 at 15:06 ANIKET KUMARANIKET KUMAR 11 bronze badge 2
  • Posts can only be of 1 type, a single post cannot be of both type psot and asd at the same time. What are you trying to fix/solve by doing this? – Tom J Nowell Commented Aug 4, 2020 at 15:45
  • Do you mean you want to duplicate the post for some reason? So the same post is copied twice, one of each type? – mozboz Commented Aug 4, 2020 at 19:58
Add a comment  | 

1 Answer 1

Reset to default 1

Your best bet would be hooking into the post type transition and creating the second (custom) post (type) via PHP. Here is a function that can accomplish just that:

// Automatically create a news (regular post in category ID 1 = news) when a wiki post (custom post type) has been published
function nxt_create_news($new_status, $old_status, $nxt_wiki_post) {
    if('publish' === $new_status && 'publish' !== $old_status && $nxt_wiki_post->post_type === 'wiki') {
        $nxt_post_author = $nxt_wiki_post->post_author;
        $nxt_wiki_permalink = get_post_permalink($nxt_wiki_id);
        $nxt_wiki_title = $nxt_wiki_post->post_title;
        $nxt_postarr = array(
            'post_author' => $nxt_post_author,
            'post_content' => 'The wiki entry ' . $nxt_wiki_title . ' has just been published.<br /><br /><a href="' . $nxt_wiki_permalink . '" title="' . $nxt_wiki_title . '">Check out wiki entry</a>.',
            'post_title' => 'The wiki entry ' . $nxt_wiki_title . ' has been published',
            'post_status' => 'publish',
            'post_category' => array(1),
        );
        $nxt_post_id = wp_insert_post($nxt_postarr);
        set_post_thumbnail(get_post($nxt_post_id), '1518');
    }
}
add_action( 'transition_post_status', 'nxt_create_news', 10, 3 );

For you, it might be the other way round. You'd create a custom post type after a regular post in some specific category has been created. I hope the example above provides enough context, but just ask if you need more input.

本文标签: pluginsSubmit posts in two different post types with the same form