Is your code inside a jQuery document ready function? You really shouldn’t need to add an action for every radio element, especially if you trying to detect a change in an element. Maybe it’s because I always extend the ACF objects when I’m going to do custom JS for ACF fields.
Hi John,
Thanks for the suggestion; unfortunately I couldn’t get it to work however.
But I did find a solution. It’s not very elegant and I’m sure there are better ways, but this functions at least:
$('#acf-field_55bf6059da1d8').focusin(function () { alert ("default") });
$('#acf-field_55bf6059da1d8-P').focusin(function () { alert ("default") });
$('#acf-field_55bf6059da1d8-D').focusin(function () { alert ("D") });
$('#acf-field_55bf6059da1d8-NP').focusin(function () { alert ("NP") });
And so on. It means a line for each option of course. And for some reason without specifying the value defaults to the first item.
you can probably do something like this
$('[data-key="field_5979fbd93d722"] input[type="radio"]:checked').change(e) {
alert(e.target.value);
}
if you extend the acf object like I do in these examples https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/unique-repeater-checkbox you can do this
alert(e.$el.val());
There are 2 parts of the function at. The first part of the function adds values and the second part deletes them. To be honest, I’m not 100% sure how the develops example does the work, I’ve never looked at it closely. My plugin does things quite a bit differently.
I have another example here that does it in yet another way https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-reciprocal-relationship.php
This worked for me and I am able to get the file type extension but now I would like to use an if statement with that extension so I can assign icons to my files based on the file type. So for example, if the filetype equals pdf then I would output a pdf icon. How can I do an if else type of statement using this?
I tried the following but no luck:
<?php $attachment_id = get_field('document_upload');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
// part where to get the filesize
$filesize = filesize( get_attached_file( $attachment_id ) );
$filesize = size_format($filesize, 2);
$path_info = pathinfo( get_attached_file( $attachment_id ) );
// show custom field
if (get_field('document_upload')): ?>
<?php echo $filesize; ?>
<?php echo $path_info['extension']; ?>
<?php
if ($path_info = "pdf") {
echo "<i class='fa fa-file-pdf-o' aria-hidden='true'></i>";
} ?>
<?php endif; ?>
Not really sure where to go from here. Thanks
Yes, all the Users being searched are only in the blog in question. Apart from me as super-admin, I suppose.
Blog ID is 2.
pre_get_users is already a key part of both methods of performing the extended user search (Better User Search and the Misha code you linked to. So, I don’t know whether it’s the problem and/or solution here.
Some learning to do, I’d say.
@hube2 Hi man! How would I solve the same problem for an gallery field?
I use
<?PHP if(get_field('fotos')) : $images = get_field('fotos'); ?>
<img src="<?php echo $images[0][sizes][home]; ?>" alt="" />
<?PHP endif; ?>
And [08-Aug-2017 16:29:17 UTC] PHP Warning: Illegal string offset ‘home’ in
If it has to do with mutlisite, when WP get’s users it only gets them from the current blog. Are all the users that you’re searching for part of the current blog? Honestly, I’m not sure what all the differences are, but it has to be something specific to multisite. All I know from a quick look at WP_User_Query is that that the blog ID alters the query in several ways. This could be something you can alter using a pre_get_users filter?
New question since I can’t find this online.
Is it possible to limit users to only fill acf_form once?
I tried but there seems to be no way to make an ACF field group display inside a WooCommerce product data tab in admin. So I resorted to a jQuery hack. It’s “ugly” but it works. The short answer is:
$("#source").appendTo("#destination");
The long answer is, create a new custom product data tab as a placeholder:
// Register New Product Data Tab
add_filter( 'woocommerce_product_data_tabs', 'add_custom_product_data_tab' );
function add_custom_product_data_tab( $tabs ) {
$tabs['my-custom-tab'] = array(
'label' => 'Dates & Times',
'target' => 'my_custom_product_data',
);
return $tabs;
}
// Create Product Data Tab Content
add_action( 'woocommerce_product_data_panels', 'add_custom_product_data_fields' );
function add_custom_product_data_fields() {
echo '<div id="my_custom_product_data" class="panel woocommerce_options_panel"></div>';
}
Then add some JS & CSS to admin:
// Add JS & CSS to move and style ACF fields inside Product Data Tab
function admin_style() {
wp_enqueue_script('admin-script', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ) );
wp_enqueue_style('admin-styles', get_template_directory_uri().'/css/style-admin.css');
}
add_action('admin_enqueue_scripts', 'admin_style');
My ‘admin.js’ file script looks like this:
jQuery(document).ready(function($) {
$("#acf-group_5988b10b578ea").appendTo("#my_custom_product_data");
});
And my ‘admin-style.css’ file looks like this:
#my_custom_product_data {
padding: 13px;
}
#my_custom_product_data .acf-label {
margin: 0;
}
#my_custom_product_data .acf-label label {
font-weight: 400;
font-size: 12px;
}
All this does is move the ACF field group from one div to another using jQuery and adds some CSS to iron out some ACF and WOO style conflicts (i.e. make the ACF panel look more like the WOO-native panels)
I really can’t help you with this, and it’s not really a but. The problem here is that you’re changing the field names. For example, if you had a field named “field_1” and you move it into a group that is named “group_1” the field’s new name is actually “group_1_field_1”. Any renaming of a field will cause the loss of data in that field. The only fix for this would be to go into the database, or to create a query, that renames all of the meta_key values and all of the acf field key references for those fields. The same thing would happen if you had a field with data in it and decided to move that field into a repeater. A group field is basically a repeater field with 1 row.
It might be possible to do this without making db changes. Lets say that you had several fields with names like
if you made a group named “hero” and then moved the three field into the group and named them
it’s possible that the values would be retained since the meta_keys will actually remain the same.
Another +1.
More than 2 years have passed since the original request. I would love to have this option in the next version of ACF. Thanks!
I’ve done some more testing and it seems that it all hangs on this:
foreach ($ads as $ad) {
// get ad_zones values from the ad
$advalues = get_field('ad_zones', $ad->ID);
}
As soon as I try to get_field()
inside my loop, it times out. I also tried using WP_Query instead of get_posts, with the same result. There are 2 posts that are being looped through so it definitely shouldn’t do this…
However, if I test it outside of the acf/load_field filter (getting the posts and their custom fields values and comparing them against all choices) – it works fine. It only times out when put inside the filter function.
This is why I initially put it outside of the filter function – it’s queueing the same field as the filter is loading (ad_zones is the field I’m trying to filter).
Still getting the same result, both with just using $field['choices'] = $result;
and the foreach loop:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in (…)/wp-includes/class-wp-query.php on line 889
When you’re editing a field group there is a little box on the right (if you haven’t removed it) with a link to send a gift.
It wasn’t so much that I didn’t think it was possible, it was more of the amount of code that might be required to get it done. A lot of complicated code would just be difficult to maintain. Luckily, I was able to find a simple, efficient way to do this. I posted the basics of what I did here https://support.advancedcustomfields.com/forums/topic/acf-repeater-sub-fields-are-losing-swapping-content-when-re-ordered/#post-52834. For my plugin it’s a little more specific, limited to only the fields that are removed, so there’s much less chance it will cause performance issues.
Okay, so I have worked myself to this functons which seem to do the trick:
function abGetAllFields($pro_num){
$tmpArr = get_field_objects($pro_num);
$fillArray = array();
foreach( $tmpArr as $tmpFieldObject ) {
$fillArray = abGetAllFieldsCycle($fillArray, $tmpFieldObject["name"], $tmpFieldObject["value"]);
}
return $fillArray;
}
function abGetAllFieldsCycle($fillArray, $name, $value) {
if (is_array($value)) {
foreach( $value as $key => $value ) {
$fillArray = abGetAllFieldsCycle($fillArray, $key, $value);
}
} else {
array_push($fillArray, [$name, $value]);
}
return $fillArray;
}
$myFieldArray = abGetAllFields($pageId);
I’m not an advanced developer but can sort of understand why it’s happening myself, the user doesn’t have write permissions to that field but requires it to write the value to another row ID in the repeater (something along those lines?)
So if I were to make the text field visible to all, but read-only to users, would this have the same issue?
Adrian
I think that it’s intended.
If the post has never been saved then the group has no rows but if it has been saved it will always have one row even if all the fields in the group have no value.
I think that using have_rows() is just a convenience so that you can reference the fields without the group field name prefix.
For example, lets say that you have a group field with the name of “group” and it has 2 sub fields; “field_1” and “field_2”.
You can access these fields like this:
the_field('group_field_1');
the_field('group_field_2');
or for the convenience of not added the field names together you can do:
while (have_rows('group')) {
the_row();
the_sub_field('field_1');
the_sub_field('field_2');
}
have_rows()
will always return true if the post has been saved.
Think of a regular repeater that has several sub fields but none of them are required. If you add a row to this repeater but do not enter any values into it the empty fields will be saved. This repeater will have one row even though no values have been entered into the existing row. The group field works the same way.
If you think this is a bug you are free to open a new support ticket https://support.advancedcustomfields.com/new-ticket/
Thanks John. I’ll mark this a duplicate question to https://support.advancedcustomfields.com/forums/topic/acf-pro-5-6-0-unable-to-display-regular-custom-fields-metabox-on-cpts/
Struggling to understand it a little bit – mainly how it builds the query AND THEN decides what fields to include in the search. But I guess that’s just SQL.
I am seeing results with a basic query.
Problem is, paginated results are only showing two per page.
In fairness, that was also a problem I experienced when using a plugin, Better User Search, which was also built to allow a user to specify custom fields to search. I am now wondering if there is another reason why results in both methods are being pegged to just two per page.
Indeed, I have just found this issue relating to the “Number of items per page” count set in screen Settings…
I’ve stripped out other functions that could be causing a conflict and isolated the one in the code you linked to…
add_action('pre_user_query','rudr_extend_user_search');
function rudr_extend_user_search( $u_query ){
// make sure that this code will be applied only for user search
if ( $u_query->query_vars['search'] ){
$search_query = trim( $u_query->query_vars['search'], '*' );
if ( $_REQUEST['s'] == $search_query ){
global $wpdb;
// let's search by users first name
$u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'first_name'";
// you can add here any meta key you want to search by
// $u_query->query_from .= " JOIN {$wpdb->usermeta} cstm ON cstm.user_id = {$wpdb->users}.ID AND cstm.meta_key = 'YOU CUSTOM meta_key'";
// let's search by all the post titles, the user has been published
// $u_query->query_from .= " JOIN {$wpdb->posts} psts ON psts.post_author = {$wpdb->users}.ID";
// what fields to include in the search
$search_by = array( 'user_login' );
// apply to the query
$u_query->query_where = 'WHERE 1=1' . $u_query->get_search_sql( $search_query, $search_by, 'both' );
}
}
}
I haven’t really used a group field before and I just did some quick testing. It appears that a group is basically a repeater with 1 row and the field always has 1 row if the post or admin page it’s on has been saved since the field in the group will always have a value, even if the value is false/null/empty or whatever.
This was going to be a very long comment but then I found something while I was looking for information and it just made sense to give you a link https://rudrastyh.com/wordpress/pre_user_query.html
Hi James, I saw this topic and I am facing the same issue as some others here. Maybe you can help me out too, here 😉
I have the following code:
<?php
foreach ( $terms as $term ) {
$image = get_field('berufe-thumb', $term->taxonomy . '_' . $term->term_id );
echo '<pre>';
echo "Image field value:";
var_dump($image);
echo '</pre>';
?>
The result is: Image field value:NULL
Could you have a look at it, please?
Use a custom location rule that overrides the ACF location rule for taxonomy https://www.advancedcustomfields.com/resources/custom-location-rules/
// priority 20 runs after ACF built in filter
add_filter('acf/location/rule_match/taxonomy', 'acf_location_rules_match_taxonomy', 20, 3);
function acf_location_rules_match_taxonomy($match, $rule, $options) {
if ($rule['param'] == 'taxonomy' && !isset($_GET['tag_ID'])) {
// if the rule is for taxonomy but $_GET['tag_ID'] is not set
// then this is the main taxonomy page and not a term page
// set match to false
$match = false;
}
return $match;
}
I’m glad you brought this up, there are many times when I don’t want fields to show on this page and your question made me want to figure out how to do it.
This is very interesting. In all the time I spent looking at input.js I never realized there was a javascript equivalent to get_field(). I’ll definitely be looking at this more closely the next time I need to build custom JS for fields. Thank you for taking the time to share your work.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.