Hassan Ali
Hassan Ali

Reputation: 593

Adding category in wordpress - custom

// Auto post ( Unique File Date ).
$postData = array(
    'post_category' => array( '0' ),
    'post_status' => $Post_Status,
    'post_type' => $Post_Type,
    'post_date' => $File_Individual_Date
);
wp_insert_post( $postData );

when using 'post_category' => array( '0' ),, it adds a post under the category Uncategorised. What if I have a category in a var lets say:

$myCategory = 'News';

then using 'post_category' => $myCategory gives error and no new post is added. Is it possible to add categories in this way?

Upvotes: 0

Views: 66

Answers (2)

Pieter Goosen
Pieter Goosen

Reputation: 9951

Check the docs for wp_insert_post

post_category should be an array of category ID's, so you would need to assign an array of category ID's to your variable for this to work

Upvotes: 1

Vick
Vick

Reputation: 287

Try $myCategory = array('News');

UPD: Just in case

Upvotes: 0

Related Questions