I am working on a code where I need to insert the array as the content in wordpress admin .
I am using below code
$newArray = array();
$new_post = array(
'post_type'=>'post',
'post_title'=>"New post created Programmatically",
'post_content'=>"$newArray"
);
wp_insert_post($new_post);
So Please guide me How can I use array as post_content in creating custom posts.
I am working on a code where I need to insert the array as the content in wordpress admin .
I am using below code
$newArray = array();
$new_post = array(
'post_type'=>'post',
'post_title'=>"New post created Programmatically",
'post_content'=>"$newArray"
);
wp_insert_post($new_post);
So Please guide me How can I use array as post_content in creating custom posts.
It depends on what type of data your array contains. But you would need to first loop your array; and create a valid string for passing into the post_content
argument.
For example, if your array consisted of text strings:
$newArray = array( 'test_1', 'test_2', 'test_3' );
$text = '';
// Loop the array and create a text string
foreach( $newArray as $string ) {
$text .= $string . ' | ';
}
Now you can insert the text into the post content.
$new_post = array(
'post_type'=>'post',
'post_title'=>"New post created Programmatically",
'post_content'=> $text
);
wp_insert_post($new_post);
'post_content'=>$newArray
$newArray not need to passed in quotes. I that defined array you can pass fields etc – jas Commented Jan 8, 2018 at 10:57