plugin development - Add an array as post content dynamically

admin2025-01-07  3

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.

Share Improve this question asked Jan 8, 2018 at 9:21 user134299user134299 1
  • You have to remove double quotes like this : '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
Add a comment  | 

1 Answer 1

Reset to default 0

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);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265135a1022.html

最新回复(0)