wp_insert_post doesnt show in WP Admin panel - php

so i want to add post programatically with my plugin, i write this code:
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_category' => array(1)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
and this is return post ID to variable but post doesnt show in wp admin posts table. WordPress is counting this like new post but doesnt show. Why?

Related

i want to insert post in specific category using wp_insert_post

i used wp_insert_post to add posts to custom post type i had created but i need to insert the posts in category , i used post_category=> array($category_id) but it didn't work .. here is the code
$my_post = array(
'post_type' => 'articles',
'post_title' => "test",
'post_content' => "blabla",
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(38),
);
wp_insert_post( $my_post );
You can set it after the post creation with:
$catid = 38;
wp_set_post_categories($my_post,$catid,false);
If you're not sure what's the category ID you can use the category slug:
$catid = get_category_by_slug( 'xxx' );
The category taxonomy, of course, should already exist in that Custom Post Type.
You can set it in the CPT definition with:
'taxonomies' => array( 'category' ),

how to edit wordpress page from backend

i have created a plugin that creates a page on activation.
now i cant find a way to update the content of the page programitically
i have been searching the internet for a while could not find an answer
here is how i create a new page using my plugin
function some_function()
{
$post_details = array(
'post_title' => 'Page title',
'post_content' => 'Content of your page',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $post_details );
}
register_activation_hook(__FILE__, 'some_function');

How to add menu link when activate the wordpress plugin?

I am developing a Wordpress plugin. I have some links for showing product Brands name and Product categories listing, in the url: http://testsite.com/product-categories and http://testsite.com/brands like that. So I want this link will be auto generated when plugin is activated and place in the default menu in Wordpress. How can I do that. I tried this code but its not working.
register_activation_hook( __FILE__, array( $this, 'myplugin_activate' ) );
public function myplugin_activate() {
$product_categories = array(
'post_content' => '',
'post_name' => 'product-categories',
'post_title' => 'Product Categories',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
$brands = array(
'post_content' => '',
'post_name' => 'brands',
'post_title' => 'Brands',
'post_status' => 'publish',
'post_type' => 'nav_menu_item',
);
wp_insert_post( $product_categories );
wp_insert_post( $brands );
}
Can anyone tell me how to do that? One thing I can not do that as page post type.
Searching for wp_insert_post and nav_menu_item, I found Programmatically add a Navigation menu and menu items where wp_create_nav_menu is suggested.
Should be a matter of using the functions wp_get_nav_menu_object, wp_create_nav_menu, wp_update_nav_menu_item and get_nav_menu_locations.

PHP WP Insert Post

I'm struggling a bit to insert a post with the features I require.
include ('../wp-load.php');
$my_post = array(
'post_title' => 'title' ,
'post_content' => 'some content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(34,35),
'tags_input' => array('tag1,tag2'),
'the_post_thumbnail' => 526
);
// Insert the post into the database
wp_insert_post( $my_post );
Question 1:
It's all working besides for 'the_post_thumbnail' => 526 - I was hoping that was going to attach the media item id (526) as the featured post image (obviously this isn't working). What is the correct way to do this?
Question 2:
Is there a way to get the URL of the post that is created?
Please try the following example that uses the functions set_post_thumbnail() and get_permalink():
$my_post = array(
'post_title' => 'title' ,
'post_content' => 'some content',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(34,35),
'tags_input' => array('tag1,tag2'),
);
// Insert the post into the database
$pid = wp_insert_post( $my_post );
if( is_wp_error( $pid ) )
{
// Display error:
echo $pid->get_error_message();
}
else
{
// Set featured image to inserted post:
set_post_thumbnail( $pid, 526 );
// Get permalink:
$link = get_permalink( $pid );
}
where we use is_wp_error() to make sure the insert was sucessful.
Hope this helps.

Cant save custom categories of a custom taxonomy

I use the code like below in a submit page.
$new_post = array(
'post_title' => $title,
'post_content' => $description,
'post_excerpt' => $excerpt,
'post_category' => array($_POST['cat']),
'tags_input' => array($tags),
'post_status' => 'publish',
'post_type' => 'custom_post_type'
);
$pid = wp_insert_post($new_post);
The $_POST['cat'] fetches the correct `category id. My custom taxonomy is named directory.
I am able to view in the saved post in the backend but the category checkbox is not checked there and in the front end i am displaying the post under these custom category, but it doesn't appear.
Is there a way to save the custom category of a custom taxonomy properly.
To assign a taxonomy to a post:
$cat_ids = array( $new_cat_id );
$cat_ids = array_map('intval', $cat_ids);
wp_set_object_terms( $post_id, $cat_ids, 'my_tax_name' );

Categories