• Hi, i have using wp_insert function to publish a post using a template, I wished to insert the same post into 2 post type

    first post type is Post
    second post type is stories

    ‘post_type’ => array(‘post’,’stories’),

    I tried using this function but I only insert in post section I want to insert the post in 2nd post type can u help me with this

    Thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    You need to implement a separate wp_insert_post() for each post type. You can also use the same array, just adjust the 2nd one.

    Rough example:

    $array = [
     'post_type' => 'post',
     'post_content' => 'Hello World'
    ];
    wp_insert_post($array);
    
    $array['post_type'] = 'stories';
    wp_insert_post($array);
    Thread Starter rajdharshini1430

    (@rajdharshini1430)

    I don’t need TO INSERT ‘post_content into stories I want to insert “post_content’ into the post and custom field meta into both post type

    Thread Starter rajdharshini1430

    (@rajdharshini1430)

    the code is https://freeimage.host/i/D3oNku in the link

    I want to insert post content in POST type and meta_input into both post and stories type

    Moderator threadi

    (@threadi)

    Then you need to build your array accordingly as you need it for each wp_insert_post(). Unfortunately you can’t copy the source code from the image to build a template for it.

    Thread Starter rajdharshini1430

    (@rajdharshini1430)

    it’s my own code for my custom template I ask if it is possible ? to insert meta_input into both post types.

    I tried but it will not work for me, help me to fix it?

    Moderator threadi

    (@threadi)

    Yes, it is possible. As already written you need 2 wp_insert_post() – one for each post type. And the array you pass to wp_insert_post() you have to build as you need it.

    Please post the relevant code here as text so that you can be shown the corrections if necessary.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Multiple Post type in wp_insert function’ is closed to new replies.