Skip Navigation

[Resolved] Stop using layouts for pages after wp_insert_post

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by Mario 6 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#603160
2018-01-03_20h16_28.png

I am trying to: Insert a new post via php.

$post_id=wp_insert_post(
         array(
            'post_content'   => "new page.",
            'post_name'      => "post name",
            'post_title'     => "message",
            'post_status'    => 'publish',
            'post_type'      => 'page'
            'page_template' =>  $new_page_template 
         )
      );

Since i use toolset-layouts with template layout for pages per default ("layout for pages"), i always get assigned this layout to my newly created page. I have to go to the interface manually and click "stop using this template layout for pages".

How could i prevent, toolset automatically assigning this template layout to this created page? I want to have a blank, native created page.
Thanks in advance for any hints.

#603513

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - _layouts_template is the post meta key that holds the Layouts template name as meta value.

So - you need to use hook wp_insert_post_data and set correct action from there:
=> https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data

For example:

function func_remove_layouts_template_connection( $post_id, $post, $update ) {
       if($post->post_type=='page'){
              	delete_post_meta( $post_id,'_layouts_template');
        }
}
add_action( 'wp_insert_post', 'func_remove_layouts_template_connection', 10, 3 );

You should adjust the above code as required.

#603535
2018-01-04_19h58_16.png

Thank you so much Minesh.
Works fine but unfortunatelly it looks like the "Content Template" is used since the Template Layout field is empty.
If i set Content Template to "None" manually it works. But how could i finally do this with a filter or post_meta_field?
I tried

delete_post_meta( $post_id, '_wp_page_template' );

, but doesn´t work.

#603706

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - view's template stored with postmeta key _views_template .

Could you please try to use following code and try to resolve your issue.

delete_post_meta( $post_id, '_views_template' );
#603734

Wonderfull and perfect. Thank you Minesh. Thats it.