skip to Main Content

Possible duplicate, but I couldn’t get anything to work by following this post, so please bear with me.

Hi, I’m using Woocommerce with Advanced Custom Fields, and the goal is the following:

Product A: Field 1, 2, 3.

Product B: Field 1, 2, 3.

I wrote custom code in the product page template to retrieve all custom fields for products and show them on their respective product page, as follows:

$ACF_field_group_ID = 48; // ACF Field Group's Post ID
$fields = acf_get_fields($ACF_field_group_ID);
?> <ul> <?php
// if it has data, then...
   if (have_rows($field['name'])) {
//echo the field's human readable format.
   echo "Title: " . $field['label'];
// loop through all the uploaded files then echo their url and icon
   while (have_rows($field['name'])) {
   the_row();
$file = get_sub_field($field['name'] . '_group');
            ?>
<?php

   echo "<li><a href=" . $file['url'] . " target='_blank'>" . $file['title'] . " <img src= " . $file['icon'] . " width='10' height='10'><a/></li> ";
                    ?>
<?php } /* /while */
} ?> </ul> <?php
            
}

but I am now trying to show Product A’s Field 1 inside Product B’s product page. As you can see from the above code I didn’t need the product’s ID in order to show the correct info, how can I have that code run on specific products to get their info so I can show that info anywhere, including on other product pages? so for example

$wanted_product = get_post_meta(87); //specific product ID

then have the code to get the needed fields.

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    Since Advanced Custom Fields provides helper functions using the WordPress query is not needed here. Only ACF's get_field() function is needed. Full code as below:

    //https://www.advancedcustomfields.com/resources/get_field/
                $shared_folder = get_field($acf_field_label, $product_id);
        
                foreach ( $shared_folder as $field_Shared ) {
                echo "<ul>" ;
    // if it has data, then...
    //Note: added _subfield, because that's how I setup my ACF repeater's subfield's name
                if ($field_Shared[$acf_field_label.'_subfield']['name']) {
                    
                echo "<li> <img class='product-file-icon' src= " . $field_Shared[$acf_field_label.'_subfield']['icon'] . " > ". "<a href=" . $field_Shared[$acf_field_label.'_subfield']['url'] . " target='_blank'>" . $field_Shared[$acf_field_label.'_subfield']['title'] . "<a/>
            </li>"  ;
                }
      echo "</ul>" ;    
    } //foreach
    

  2. you should query posts with ACF, Docs: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    Fast example:

    <?php 
    $args = array(
    'p'         => 42, // ID of a page, post, or custom type
    'post_type' => 'any'
    );
    
    $the_query = new WP_Query($args);
    if( $the_query->have_posts() ): ?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <img src="<?php the_field('event_thumbnail'); ?>" />
                <?php the_title(); ?>
            </a>
        </li>
    <?php endwhile; ?>
    </ul>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search