Support

Account

Home Forums General Issues Populate ACF select field\'s choices with published WooCommerce Products Reply To: Populate ACF select field\'s choices with published WooCommerce Products

  • To populate the choices you need to do a query to get all of the products. I’m not including everything from the page you posted, just the query

    
    $choices = array();
    $products = new WP_Query(array(
      'post_type' => 'product',
      'post_per_page' => -1,
      'orderby' => 'post_title',
      'order' => 'ASC'
    ));
    if ($products->have_posts()) {
      global $post;
      while ($products->have_posts()) {
        $products->the_post();
        $choices[$post->ID] = $post->post_title;
      }
      wp_reset_postdata();
    }
    $field['choices'] = $choices;
    

    for more information see https://codex.wordpress.org/Class_Reference/WP_Query