+3 votes
1.4k views
asked in Programming by (10.6k points)

I could not run wp_insert_post function in a loop, it inserts first post and does not insert other posts, how can I add multiple posts in a php file?


1 Answer

+4 votes
answered by (9.2k points)

Firstly create a php file in root folder of wordpress directory

Include wp-load.php ->

<?php include "wp-load.php"; ?>

and

$posts = array(); /* hold loop posts in  an array */
$j = 0;

for($i=1; $i<10; $i++)
{
 
$new_ date= mktime(23,31,0,9,2,2014); /* hour, minute, second, month, day, year */
$new_ date=date("Y-m-d G:i:s", $new_ date);
 
$new_post = array(
'post_title' =>  $title,
'post_content' => $text,
'post_status' => 'publish', /* or future*/
'post_date' => $new_date,
'post_author' => 1, /* author id*/
'post_type' => 'post', /* post type*/
'post_category' => array($category_id) /* you can use multiple categories so : array(1,3,5) */
);
 
 $posts[$j] = $new_post;
 $j++;
}
 
foreach ($posts as $postx) 
{

wp_insert_post($new_post);
}


Ask a Question
Welcome to WikiTechSolutions where you can ask questions and receive answers from other members of the community.

You can ask a question without registration.

Categories

...