Hello,
I used the this ACF tutorial/code to dynamically populate select fields from a repeater field in an option page. It works intermittently, but then stops working altogether. The labels and values dynamically populate the select field inside the ACF > Field Groups screen, but they do not appear at all inside the Options page select.
Here is the code I’m using:
function acf_load_color_field_choices( $field ) {
// Reset choices
$field['choices'] = array();
// Check to see if Repeater has rows of data to loop over
if( have_rows('global_colors', 'option') ) {
// Execute repeatedly as long as the below statement is true
while( have_rows('global_colors', 'option') ) {
// Return an array with all values after the loop is complete
the_row();
// Variables
$value = get_sub_field('global_color');
$label = get_sub_field('global_color_name');
// Append to choices
$field['choices'][ $value ] = $label;
}
}
// Return the field
return $field;
}
add_filter('acf/load_field/name=primary_color', 'acf_load_color_field_choices');
add_filter('acf/load_field/name=secondary_color', 'acf_load_color_field_choices');
Repeater field name is global_colors
Subfields are global_color
and global_color_name
Select Field Names to grab the repeater field(s) are:
primary_color
and secondary_color
Turns out it was due to an issue including variables from an external file. Once I cleaned things up there, it worked.
Got it figured out. Was a theme conflict.
Here is a video of the issue.
Here is a video of the issue.
Thanks John.
Thanks John!
Ah, that makes sense. For reference, I’m using Hello Elementor theme.
Quick addendum to this thread because I used it today to update the site_icon
. It seems the custom_logo
is set by using theme_mod
, not update_option
(did not work for me).
The complete code for updating the website logo should be:
set_theme_mod('custom_logo',get_field('field_name','options', false), true);
Hopefully this helps someone.
Perfection! Thank You John.
Thanks very much for the help John!
This almost gets me there, however, I’m missing the quotes around each month. Currently what gets returned is this:
Aug-22, Sep-22, Oct-22, Nov-22, Dec-22, Jan-23, Feb-23, Mar-23, Apr-23, May-23, Jun-23, Jul-23
However, I need it to have quotes around each date as well (with the comma outside) like this:
‘Aug-22′,’Sep-22′,’Oct-22′,’Nov-22′,’Dec-22′,’Jan-23′,’Feb-23′,’Mar-23′,’Apr-23′,’May-23′,’Jun-23′,’Jul-23’
The very last value should have no comma.
Forum won’t let me edit my last post for some reason. In fact, it disappeared. Here is the complete solution that’s working:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
)
)
);
$query = new WP_Query( $args );
Thank you John. Below is the complete solutionmodel for anyone looking for this setup and/or solution in the future:
'value' => '"'.$current_user->ID.'"',
had to be 'value' => $current_user->ID,
or 'value' => get_current_user_id(),
'taxonomy_query' => array(...
had to be 'tax_query' => array(...
'field' => 'term_id',
had to be added to the tax_query arguments
Complete solution:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
)
)
);
$query = new WP_Query( $args );
When I plug in the correct values for taxonomy and key, it simply returns all products. Also when I try and just echo the $terms alone to see what they’re returning, it just says “Array.”
Here is the code:
$args = array(
'taxonomy' => 'product_cat',
'meta_query' => array(
array(
'key' => 'category_user',
'value' => '"'.$current_user->ID.'"',
'compare' => 'LIKE',
)
),
'fields' => 'ids' // return just a list of term IDs
);
$terms = get_terms( $args );
$args = array(
'post_type' => 'product',
'taxonomy_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $terms,
)
)
);
$query = new WP_Query( $args );
Thank you for the reply. I appreciate it. Last night before your response, I realized what you said might be the case and I have to admit I spent hours trying to figure it out to no avail. What would a query like the one you’ve described look like?
I tried the below but it is not working.
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'taxonomy' => 'product_cat',
//Tries to show products from categories where current user ID is in the category_user field
'meta_query' => array(
array(
'key' => 'category_user',
'value' => '"' . $current_user->ID . '"',
'compare' => 'LIKE'
)
)
);
Also, I failed to mention that the User custom field name is category_user
Guess not spyglassweb. I’m looking to do the same unfortunately…
Thank you for your response. I’m new to this this. How would something like what you suggested look?
Here is the Loop page that contains the code:
<?php
global $qode_options_elision;
?>
<style>
.icon_whitepaper.brospec:before {
background-image: url(/wp-content/uploads/2019/10/brospec.png);
height: 89px;
}
</style>
<?php
$pdf_doc = get_field( 'bro-spec-pdf', $post->ID );
$pdf_url = $pdf_doc[ 'url' ];
$pdf_name = $pdf_doc[ 'filename' ];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post_content_holder icon_whitepaper brospec">
<div class="post_text">
<h2><a href="<?php echo $pdf_url; ?>" title="<?php the_title_attribute(); ?>" id="brospec-link" class="brospec-link" target="_blank">
<?php the_title(); ?>
</a></h2>
<?php the_excerpt(); ?>
<div class="posted-readmore">
<span style="font-weight:bold;"><?php
$terms = get_the_terms( $post->ID, 'brochure_category' );
foreach ( $terms as $term ) {
echo $term->name;
}
?></span>,
Posted <span class="date">
<?php the_time('F j, Y'); ?>
</span> | <a href="<?php echo $pdf_url; ?>" id="brospec-link" title="<?php the_title_attribute(); ?>" class="brospec-link" target="_blank">
<?php _e('DOWNLOAD','qode'); ?>
</a>
</div>
</div>
</article>
<!-- #download-<?php the_ID(); ?> -->
I’ve looked for hours for a straightforward tutorial where I can accomplish something which on its face seems like it would be rather simple. Unfortunately, all the posts and info I’ve found have not quite been what I’m looking for and I’m failing miserably. I’ve seen many about update_field, etc, but the info is too general for my working knowledge (eg: https://www.advancedcustomfields.com/resources/update_field/). I’m hoping someone will be able to supply what I need to accomplish this [now frustrating] task.
Here is what I’m trying to do…
I have a custom post type named ‘brochures’ and a “File” custom field (‘bro-spec-pdf’) which I’m using to echo the [‘url’] for a PDF download in a loop file (code below; listing_brochures-loop.php).
I also have a second custom field (‘bro-spec-views’; currently a number field) I would like to use as a “Views” count so I know how many times that File[‘url’] field has been clicked for each post. I would like that count to increase 1 for each click.
I understand that admin-ajax has to be used somehow, but all that I’ve tried has not worked. It’s also not my strong area, so it’s possible I just have no idea what I’m doing for that aspect.
Can someone please lend a hand and provide what’s needed to get this done? Thank you in advance for any help you can provide.
Hello,
I’ve looked for hours for a straightforward tutorial where I can accomplish something which on its face seems like it would be rather simple. Unfortunately, all the posts and info I’ve found have not quite been what I’m looking for and I’m failing miserably. I’ve seen many about update_field, etc, but the info is too general for my working knowledge (eg: https://www.advancedcustomfields.com/resources/update_field/). I’m hoping someone will be able to supply what I need to accomplish this [now frustrating] task.
Here is what I’m trying to do…
I have a custom post type named ‘brochures’ and a “File” custom field (‘bro-spec-pdf’) which I’m using to echo the [‘url’] for a PDF download in a loop file (code below; listing_brochures-loop.php).
I also have a second custom field (‘bro-spec-views’; currently a number field) I would like to use as a “Views” count so I know how many times that File[‘url’] field has been clicked for each post. I would like that count to increase 1 for each click.
I understand that admin-ajax has to be used somehow, but all that I’ve tried has not worked. It’s also not my strong area, so it’s possible I just have no idea what I’m doing for that aspect.
Can someone please lend a hand and provide what’s needed to get this done? Thank you in advance for any help you can provide.
Loop page that contains the
<?php
global $qode_options_elision;
?>
<style>
.icon_whitepaper.brospec:before {
background-image: url(/wp-content/uploads/2019/10/brospec.png);
height: 89px;
}
</style>
<?php
$pdf_doc = get_field( 'bro-spec-pdf', $post->ID );
$pdf_url = $pdf_doc[ 'url' ];
$pdf_name = $pdf_doc[ 'filename' ];
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post_content_holder icon_whitepaper brospec">
<div class="post_text">
<h2><a href="<?php echo $pdf_url; ?>" title="<?php the_title_attribute(); ?>" id="brospec-link" class="brospec-link" target="_blank">
<?php the_title(); ?>
</a></h2>
<?php the_excerpt(); ?>
<div class="posted-readmore">
<span style="font-weight:bold;"><?php
$terms = get_the_terms( $post->ID, 'brochure_category' );
foreach ( $terms as $term ) {
echo $term->name;
}
?></span>,
Posted <span class="date">
<?php the_time('F j, Y'); ?>
</span> | <a href="<?php echo $pdf_url; ?>" id="brospec-link" title="<?php the_title_attribute(); ?>" class="brospec-link" target="_blank">
<?php _e('DOWNLOAD','qode'); ?>
</a>
</div>
</div>
</article>
<!-- #download-<?php the_ID(); ?> -->
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.