Support

Account

Home Forums Add-ons Gallery Field meta_input wp_insert_post acf gallery

Solved

meta_input wp_insert_post acf gallery

  • Hello
    help me please
    I used wp_insert_post to create new post in cpt
    what needs to be sent to the field “meta_input” add an image to ACF Gallery in post?

    My code like this
    `
    $advert_name = $_POST[‘advert_name’];
    $advert_phone = $_POST[‘advert_phone’];
    $advert_new = [
    ‘ID’ => ”,
    ‘post_type’ => ‘offers’,
    ‘meta_input’ => [
    ‘name’ => $advert_name,
    ‘phone’ => $advert_phone,

    ‘gallery’ => ???????
    ],
    ‘post_content’ => $advert_desc,
    ‘post_title’ => $advert_title,
    ‘post_status’ => ‘pending’
    ];

    $post_id = wp_insert_post($advert_new);`

    https://prnt.sc/1x41arc

  • ACF has two fields that must be present. These are the field value and field key reference.

    So for example let’s say the the field name is “gallery” and the field key is “field_123456”

    A gallery field is an array of the attachment ID values from the media folder

    
    'meta_input = array(
      'gallery' => array($attachment_id), // field value
      '_gallery' => 'field_123456' // field key reverence
    )
    
  • Cool thank you so much
    and how send files to wp media library from custom form (no plugin)?
    drag and drop plugins maybe
    help please

  • if the image has a URL then you can use https://developer.wordpress.org/reference/functions/media_sideload_image/

    If this is not the case then you would need to get the image file from $_FILES in PHP and manually add them to the image folder. https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html

  • Hello
    Help me please
    I used wp_insert_post to create new post in cpt
    what needs to be sent to the field “meta_input” add to Data Picker in ACF?
    when I send in data (ex 2029) POST all right – https://prnt.sc/1x73dvk
    but in new post acf field (Data Picker) only 1970 – https://prnt.sc/1x72yu8
    for other fields all ok

    My code like this

    $advert_name = $_POST[‘advert_name’];
    $advert_rent_date = $_POST[‘advert_rent_date’];
    
    $advert_new = [
    ‘ID’ => ”,
    ‘post_type’ => ‘offers’,
    ‘meta_input’ => [
    ‘name’ => $advert_name,
    
    ‘date’ => $advert_rent_date
    ],
    ‘post_content’ => $advert_desc,
    ‘post_title’ => $advert_title,
    ‘post_status’ => ‘pending’
    ];
    
    $post_id = wp_insert_post($advert_new);
  • As I said in my first reply. You also need to set the field key reference. You are setting “name” and “date”. your also need to set “_name” and “_date” with the correct field keys. The value for “name” is working only because it is a simple text type field. A date field will not work correctly without the field key reference. In addition to this the date must be in the format YYYYMMDD

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.