admin管理员组

文章数量:1192337

I want to insert post with wp_insert_post and add meta_input in it.

I have to send my data with array but I have problem to send more than one

Here is my code :

          wp_insert_post( array(
          'post_title'    => $post['title'],
          'post_type'     => 'werknemers',
          'meta_input'    => array(
            foreach($movies as $movie) {
              $i++;
              array(
                  'key'   => 'title' . $i,
                  'value' => $movie['title']
              ),
              array(
                  'key'   => 'qty' . $i,
                  'value' => $movie['qty']
              ),
              array(
                  'key'   => 'desc' . $i,
                  'value' => $movie['desc']
              )
            }
          )
      ) );

I change it to this but now it doesn't send values of array to database correctly

          foreach($movies as $movie) {
        $i++;
        $post_args = array(
          array(
              'key'   => 'title' . $i,
              'value' => $movie['title']
          ),
          array(
              'key'   => 'qty' . $i,
              'value' => $movie['qty']
          ),
          array(
              'key'   => 'desc' . $i,
              'value' => $movie['desc']
          )
        );
      }



      wp_insert_post( array(
          'post_title'    => '1',
          'post_type'     => 'departure',
          'meta_input'    => $post_args
      ) );

I want to insert post with wp_insert_post and add meta_input in it.

I have to send my data with array but I have problem to send more than one

Here is my code :

          wp_insert_post( array(
          'post_title'    => $post['title'],
          'post_type'     => 'werknemers',
          'meta_input'    => array(
            foreach($movies as $movie) {
              $i++;
              array(
                  'key'   => 'title' . $i,
                  'value' => $movie['title']
              ),
              array(
                  'key'   => 'qty' . $i,
                  'value' => $movie['qty']
              ),
              array(
                  'key'   => 'desc' . $i,
                  'value' => $movie['desc']
              )
            }
          )
      ) );

I change it to this but now it doesn't send values of array to database correctly

          foreach($movies as $movie) {
        $i++;
        $post_args = array(
          array(
              'key'   => 'title' . $i,
              'value' => $movie['title']
          ),
          array(
              'key'   => 'qty' . $i,
              'value' => $movie['qty']
          ),
          array(
              'key'   => 'desc' . $i,
              'value' => $movie['desc']
          )
        );
      }



      wp_insert_post( array(
          'post_title'    => '1',
          'post_type'     => 'departure',
          'meta_input'    => $post_args
      ) );
Share Improve this question edited Sep 8, 2022 at 12:44 Mehran Pourjenabi asked Sep 8, 2022 at 11:19 Mehran PourjenabiMehran Pourjenabi 111 silver badge5 bronze badges 3
  • you can't put a for loop inside an array like that, you need to do the loop before that and assign it to a variable. Arrays and other values can only contain values. This doesn't look like a WordPress problem but a PHP programming problem. I'd suggest going over variables/arrays/loops on php.net – Tom J Nowell Commented Sep 8, 2022 at 11:58
  • @TomJNowell I put them outside of array and add to an variable but still have problem – Mehran Pourjenabi Commented Sep 8, 2022 at 12:07
  • In your 2nd code snippet, you're redefining $post_args for every $movie in $movies. Set $post_args = array(); before the foreach() begins, and then you can add items to it. – Pat J Commented Sep 8, 2022 at 14:43
Add a comment  | 

1 Answer 1

Reset to default 0

In your 2nd code snippet, you're redefining $post_args for each $movie. Try this instead:

$post_args = array();
foreach($movies as $movie) {
    $i++;
    $post_args[] = array(
      array(
          'key'   => 'title' . $i,
          'value' => $movie['title']
      ),
      array(
          'key'   => 'qty' . $i,
          'value' => $movie['qty']
      ),
      array(
          'key'   => 'desc' . $i,
          'value' => $movie['desc']
      )
    );
  }

  wp_insert_post( array(
      'post_title'    => '1',
      'post_type'     => 'departure',
      'meta_input'    => $post_args
  ) );

本文标签: wp insert postMultipe array in metainput