In my case, Ninja Forms conflicts with ACF.
Yep, you’re right @acf-support . I was following the docs for the latest version of Select2 instead of the version in use by ACF.
Thanks for the suggestion Jonathan.
I want to pre-fill a select field within a repeater row, based off the same sub-field in the previous row, before any of the data is saved to the database so JavaScript is the only option I can see.
I noticed the select2_args
filter on the bottom of the Adding custom javascript to fields page, but its documentation is minimal. I assume $select
is the current field, but args
and settings
are not clear.
Can you provide more detail about this filter and how it works?
Thanks for the troubleshooting tips Jonathan. I made some tweaks to my selectors and with the following code was able to give the Select2 field focus when a new row is added.
acf.add_action('append', function( $el ){
var $wood = $el.find( '[data-name="bulk_wood"] select');
$wood.focus();
$wood.select2('open');
});
I’m still unable to set the value of the Select2 field though. This next snippets work as expected setting the value of text inputs using data from the previous row. Is it possible to set the value of a Taxonomy field type with the Select2 appearance? I think the AJAX parts are complicating this task.
acf.add_action('append', function( $el ){
var $prev_row = $el.prev();
var $wood = $prev_row.find( '[data-name="bulk_wood"] select' ).val();
var $thickness = $prev_row.find( '[data-name="bulk_thickness"] input' ).val();
$el.find( '[data-name="bulk_thickness"] select' ).append(new Option('text', 'value')); // Not working
$el.find( '[data-name="bulk_thickness"] input' ).val( $thickness ); // Works great
});
This is my matching function. It functions correctly but only on page load. Is anyone able to suggest how to get custom taxonomy terms into the $options array so the function can evaluated on changes to the custom taxonomy.
function acf_location_rules_match_taxonomyTerm( $match, $rule, $options ){
// vars
$taxonomies = get_object_taxonomies( $options['post_type'] );
$terms = $options['taxonomy'];
// not AJAX
if( !$options['ajax'] ){
// no terms? Load them from the post_id
if( empty($terms) ){
if( is_array($taxonomies) ){
foreach( $taxonomies as $tax ){
$all_terms = get_the_terms( $options['post_id'], $tax );
if($all_terms){
foreach($all_terms as $all_term){
$terms[] = $all_term->term_id;
}
}
}
}
}
if($rule['operator'] == "<==>"){
$match = false;
if($terms){
$current_terms = get_the_terms($options['post_id'], $rule['value']);
if ( $current_terms && ! is_wp_error( $terms ) ) {
$current_term_ids = array();
foreach ($current_terms as $current_term) {
$current_term_ids[] = $current_term->term_id;
}
}
foreach ($current_term_ids as $current_term_id) {
if( in_array($current_term_id, $terms) ){
$match = true;
}
}
}
}
else{
$match = false;
}
}
return $match;
}
I have added custom types, operators and values to the location rule row by following the tutorial.
In my matching rule I’m unable to see any custom taxonomies in the $options array.
Do I have to do anything to add custom taxonomies to the $options array?
Hey Fredrik. Yes I tried setting the location rule to Taxonomy Term
but that makes the fields related to the taxonomy and visible on the “edit taxonomy terms” page.
I’m aiming to have my custom fields related to posts and visible on the post edit page when a term from my custom taxonomy is selected.
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.