Hey ckka,
So, the first thing you’ll need to do is determine what product type you’re looking at, so in your function, you could do something like this:
$product_type = get_field('product_type'); // Or whatever you've named your select field
Then, if you’ve got a ‘Combo’ product type, you’ll need to loop through each of the chosen post objects:
if ( $product_type == 'Combo' )
{
// get the Posts and deal with them here
$combination = get_field( 'combination' ); // or whatever you've named your repeater
if ( ! empty( $combination ) )
{
foreach ( $combination as $combination_item )
{
// each item is returned as an array with a key of 'post':
$post_id = $combination_item['post'];
}
}
}
else
{
// deal with the single item here
}
So, to put it all together, it might look something like this:
function prepare_stack( $field )
{
$page = get_the_ID();
$hidden = '';
$value = '
<div class="stack">';
$product_type = get_field( 'product_type' );
if ( $product_type == 'Combo' )
{
$combination = get_field( 'combination' );
if ( ! empty( $combination ) )
{
foreach( $combination as $combination_item )
{
$post_id = $combination_item['post'];
$value .= '
[acf field=\'pd_snip\' post_id=\''. $post_id . '\']';
$hidden .= '
[acf field=\'sc_text_link\' post_id=\''. $post_id . '\']';
}
}
}
else
{
$value .= '
[acf field=\'pd_snip\' post_id=\''. $page . '\']';
$hidden .= '
[acf field=\'sc_text_link\' post_id=\''. $page . '\']';
}
$value .= '
<b>[acf field=\'pd_price\' post_id=\''. $page . '\']</b>
[acf field=\'sc_add_to_cart_button\' post_id=\''. $page . '\']';
$value .= '
<span class="hide">'.$hidden.'
</span>';
$value .= '
</div>
';
$field['value'] = $value;
return $field;
}
add_filter('acf/prepare_field/key=field_5a64f5e14375a', 'prepare_stack');
You can see I used a variable called $hidden while I was looping through the combination posts so I could add it in later.. this means I don’t have to loop through twice.
This code is untested, so you should go through it line by line to make sure it does what you need it to do!
Hope this helps..
Hey edmundcwm,
I’m not familiar with the Listify theme, but that variable ( {{data.id}} ) looks like something from a templating language rather than a regular PHP variable. It’s probably converted to the ID after all the PHP work is done (including get_field ).
WordPress has a function to get the ID that may be available to you in the template you are working on. Maybe give this a try:
$post_id = get_the_ID();
echo get_field( 'field_name', post_id );
.. or post a bit more of your code so we can get a better sense of what’s going on!
Hope this helps.
Hey shanekins,
You seem to be pretty close with this.. assuming that the dropdown you have created is returning the term ID, all you need to do is use the built in WordPress function ‘get_term’ to get the taxonomy’s name:
function my_save_post( $post_id ) {
// Pull the Term ID from the form:
$term_id = $_POST['acf']['field_59940ffa4a644'];
// Now get the WordPress term Object:
$term = get_term( $term_id );
// Check to see that a term is returned successfully
if ( ! empty( $term && ! is_wp_error( $term ) ) )
{
// Now you can get the term name for your title
$title = $term->name;
$new_post = array(
'ID' => $post_id,
'post_status' => 'publish',
'post_title' => $title,
'post_type' => 'points'
);
remove_action('acf/save_post', 'my_save_post', 20);
wp_update_post( $new_post );
add_action('acf/save_post', 'my_save_post', 20);
}
}
add_action('acf/save_post', 'my_save_post', 20);
Hey Nubee..
So close! But its actually just a bit simpler.. your meta ‘key’ only needs to be the name of the group, then underscore, then the name of the sub-field… so in your case:
$metaQuery = [
'relation' => 'AND',
[
'key' => 'stamps_local',
'compare' => '=',
'value' => true,
],
];
Hope this helps!!
Hey Derk…
You’re pretty close here.. but the object you’ll use to prepopulate the ACF field is the value of the field (for each post), rather than the field itself..
Perhaps something like below:
function my_acf_import_address( $value, $post_id, $field )
{
// Only do this if there is not a google_map
// value already in this post's fields
if ( empty( $value ) )
{
// Here I'm assuming the old map function stored
// the address, lat & lng as post meta & not as custom fields
$address = get_post_meta( $post_id, '_job_location', true );
$lat = get_post_meta( $post_id, 'geolocation_lat', true );
$lng = get_post_meta( $post_id, 'geolocation_long', true );
// Now we assign the returned meta values to the
// value array to be returned
$value['address'] = $address;
$value['lat'] = $lat;
$value['lng'] = $lng;
}
return $value;
}
add_filter('acf/load_value/name=google_map', 'my_acf_import_address', 10, 3);
Hope this helps!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.