I have the following code in a PHP function that is activated when I install my plugin that lets you create a post or page.
Works perfect and make the page if the $post_type is "post", but if the $post_type is "page", then it does not work, does not create the page.:
$my_post = array(
'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0
);
wp_insert_post( $my_post );
What is the problem? I can not find the solution.
Thank you very much!
I think you have to set the guid too, like this
$PageGuid = site_url() . "/my-page-req1";
$my_post = array( 'post_title' => 'My page Reql',
'post_type' => 'page',
'post_name' => 'my-page',
'post_content' => 'This is my page reql.',
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'menu_order' => 0,
'guid' => $PageGuid );
$PageID = wp_insert_post( $my_post, FALSE ); // Get Post ID - FALSE to return 0 instead of wp_error.
Related
Can't seem to find a definitive answer anywhere. I need to get the ID of a post after it's created with wp_insert_post().
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
wp_insert_post( $log_item );
After that, how do I get the ID of the just created $log_item post?
Please store the post id into temporary variable :
$log_item = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'log-item',
'meta_input' => array(
// 'wpcf-date-checked' => '',
// 'wpcf-checked-by' => '',
'wpcf-belongs-to-id' => $parent_id,
),
);
// You can also get the new post ID after inserting a new post:
$post_id = wp_insert_post( $log_item , $wp_error );
For more help : Click Here
I can create a new category using wp_insert_category but I can not add it to my post, any suggestion please!
$cat = array(
'cat_name' => 'dossiers-a-suivre',
'cat_slug' => 'dossiers-a-suivre',
'taxonomy' => 'category' );
$cat_id = wp_insert_category( $cat );
$my_post = array(
'post_title' => "post test",
'post_content' => 'This is my post.',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'folder',
'post_category' => array( $cat_id)
);
$post_id = $this->insert_post($my_post);
I solved the problem by using wp_set_object_terms :)
$cat = array(
'cat_name' => 'dossiers-a-suivre',
'cat_slug' => 'dossiers-a-suivre',
'taxonomy' => 'category' );
$cat_id = wp_insert_category( $cat );
$my_post = array(
'post_title' => "post test",
'post_content' => 'This is my post.',
'post_date' => date('Y-m-d H:i:s'),
'post_type' => 'folder',
'category_name' => 'dossiers-a-suivre',
);
$post_id = $this->insert_post($my_post);
wp_set_object_terms($post_id, $cat_id, 'category' );
Try please wp_set_post_terms or wp_set_object_terms
I wrote a script to add new entries into my "interactions" post type, but they don't come up in interactions, and when I manually point my browser to the ID of the post in the editor, it tells me "Unknown post type".
Heres the code I added the post with:
$new_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => '',
'post_type' => 'interaction'
);
$id = wp_insert_post($new_post);``
The post is being created but the new entries don't show up on the interactions list and I can't edit it because it tells me its an unknown post type.
What could be causing this issue?
You have an typo in your post_type parameter. Change it to: interactions instead of interaction
<?php
$new_post = array(
'post_title' => $title,
'post_content' => '',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => '',
'post_type' => 'interactions'
);
$id = wp_insert_post($new_post);
I've created a plugin for wordpress that can auto post pages on demand
my problem is formatting this pages to a specific template
I need that when the post page is auto inserted in the DB, set the Sidebar position to disabled in order to have a full width page and hide the page title... this options appear in the dashboard and I can click on them one by one, but that has to be automatic, not manually.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
By the way, the page_template attribute ('page_template' => 'microsite') doesn't work either. Te post is inserted but the template is set to default.
Thanks in advance!!!
I just go through you code, you have done a simple mistake, in "page_template" that should be a file name (filename.php) of your template not a template name . for ex. if your template microsite's file name is microsite.php than your code will be.
$my_post = array(
'post_title' => wp_strip_all_tags( $tituloFichaP1 ),
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'page',
'post_parent' => $parentPost,
'page_template' => 'microsite.php',
'comment_status' => 'open',
);
wp_insert_post( $my_post );
Hope this will help you.
I have developed a plugin for creating automatic page which containing the following code:
$new_page = array(
'slug' => $_REQUEST[sl_store],
'title' => $_REQUEST[sl_store],
'content' => "$_REQUEST[sl_store] <br> $_REQUEST[sl_address]"
);
$new_page_id = wp_insert_post( array(
'post_title' => $new_page['title'],
'post_type' => 'page',
'post_name' => $new_page['slug'],
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $new_page['content'],
'post_status' => 'publish',
'post_author' => 1
));
The page is creating fine. However, I need to fetch the "GUID" value for the newly created page from the DB.
Can anyone help me?
There's a get_the_giud() function (see here). So
get_the_guid( $new_page_id );
should do what you need.