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
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:you should query posts with ACF, Docs: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
Fast example: