How to add new elementor post programmatically?

Member

by deron , in category: PHP CMS , a month ago

How to add new elementor post programmatically?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by edmond_brakus , a month ago

@deron 

To add a new Elementor post programmatically, you can use the following steps:

  1. Include the necessary files and functions:
1
2
3
require_once ABSPATH . 'wp-admin/includes/post.php';
require_once ABSPATH . 'wp-admin/includes/taxonomy.php';
require_once ABSPATH . 'wp-admin/includes/media.php';


  1. Create a new post using wp_insert_post() function:
1
2
3
4
5
6
7
8
$new_post = array(
    'post_title' => 'New Elementor Post',
    'post_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    'post_status' => 'publish',
    'post_type' => 'post'
);

$post_id = wp_insert_post($new_post);


  1. Assign the Elementor template to the post using update_post_meta():
1
2
update_post_meta($post_id, '_elementor_template_type', 'single');
update_post_meta($post_id, '_elementor_edit_mode', 'builder');


  1. Set the Elementor template ID for the post using update_post_meta():
1
2
$elementor_template_id = 123; // Replace with the ID of the Elementor template you want to use
update_post_meta($post_id, '_elementor_template_id', $elementor_template_id);


  1. Reload rewrite rules to update the post URL:
1
flush_rewrite_rules();


By following these steps, you can programmatically add a new Elementor post in WordPress. Just make sure to replace the placeholder values with your actual data.