Support

Account

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

Solved

Populate ACF select field\'s choices with published WooCommerce Products

  • I currently have a select field on a field group that needs to be populated with name of the published woocommerce products and have the product ID as the value. It will be like this

    <option value="{product_id}">{Product name}</option>

    I checked https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/ but could not get it to work with the product names and ID

    Please let me know how I can achieve this.

    Best Regards
    Love for the plugin <3

  • 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

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

The topic ‘Populate ACF select field\'s choices with published WooCommerce Products’ is closed to new replies.