@bryan-orozco I had been looking ALL over for this, thank you!
This fellow explains how to use javascript to restrict the dates of the DatePickers, just used it in a project of mine:
https://www.damiencarbery.com/2021/10/acf-date-time-picker-set-end-date-to-after-start-date/
@hube2 Actually, that works perfectly, thank you.
I am not sure you can set a default image.
I think that your template should take care of this for you. I would just setup ACF option with the default image (or define a variable in your template if it’s not something that your client needs to change).
When you are displaying the logo, you can do something like this:
if ( '' === get_field( 'logo' ) {
$image_url = get_field( 'default_image', 'option' );
// Render the default image.
}
Non option version:
$default_image = (your default image URI);
if ( '' === get_field( 'logo' ) {
// Render the default image using $default_image
}
Pages are just posts, so as long as you use get_field with a post id (assuming you are not in the loop) you should be fine.
For instance:
$field_value = get_field( 'my_awesome_field', $page_id );
Thanks John.
af_registration is class from WPMUDEV which I have been using for years and it works fine. All my actions/filters are using that, so I would see 2 of everything, so who knows.
I was very naughty and stuck the button inside a div and hid the second one with a CSS rule 🙈
.btn-container {
&:last-child {
display: none;
}
}
What was the problem? The date format needed to be Ymd?
Hi!
Thanks to this post i was able to figure out what was going on. In case it helps anyone out there:
In my website settings I have defined a series of one-column repeaters that hold values that the user can enter. When I was getting these options in hooks that attached to ‘acf/load_field/key=’ getting the repeater yielded the expected behaviour, but sometimes it just returned the size of the repeater.
To get around this, I wrote this code that takes in the repeater name and the column you are after. It uses the ACF API when possible or otherwise falls back on the WordPress API to obtain the value.
Makes me wonder if I should just stick to the WP API to improve performance? 🤔
public function get_repeater_option_values( $repeater_name = '', $repeater_column_name = '' ) {
$associative_array = array();
$repeater_instance = \get_field( $repeater_name, 'option' );
if ( \is_array( $repeater_instance ) ) { // get_field( 'repeater_name', 'option') gets a repeater instance.
$repeater_instance = \get_field( $repeater_name, 'option' );
$setting_values = array_column( $repeater_instance, $repeater_column_name );
foreach ( $setting_values as $key => $value ) {
$associative_array[ str_replace( ' ', '_', strtolower( $value ) ) ] = $value;
}
} else { // get_field( 'repeater_name', 'option' ) returns the repeater size.
$repeater_size = $repeater_instance;
for( $i = 0; $i < $repeater_size; $i++ ) {
$value = \get_option( "options_{$repeater_name}_{$i}_{$repeater_column_name}" );
$associative_array[ str_replace( ' ', '_', strtolower( $value ) ) ] = $value;
}
}
return $associative_array;
}
@hube2 thanks for the reply.
ACFBuilder generate different keys for all of the controls, so that’s not the case.
I looked at making the field read only, but it all looked complicated since it’s a select2 field. Maybe I’ll persevere if I have the time.
(update: found this link, traviskelleher posted something that might work)
Thanks!
Well…talk about ugly. What I did was after figuring out if it was a new post or an edit, I created a third tab and placed the selects (that I don’t want the user to see) inside the tab. I then remove the tab by means of jQuery and everything works as expected.
If anyone has a better solution than this, please chip in. One of my selects calls an endpoint to load its values, I would love to get rid of this overhead when editing the post as that select is no longer needed. Thanks!
if ( PostHelper::is_current_page_new_post() ) {
$afb->add_adoption_type_select_field();
$afb->add_adoption_animal_select_field();
$afb->add_adoption_person_select_field();
} else {
$afb->add_adoption_type_text_field();
$afb->add_adoption_animal_text_field();
$afb->add_adoption_person_text_field();
}
$afb->add_tab_field( 'Hidden', 'hidden' );
if ( ! PostHelper::is_current_page_new_post() ) {
$afb->add_adoption_type_select_field();
$afb->add_adoption_animal_select_field();
$afb->add_adoption_person_select_field();
}
Do you have anything in your console output? Any jQuery errors?
@hube2 yes, it’s kind of hard to know what it refers to, my guess is the order ID of the WC order that was just placed.
@squatter1 I would rule out that your hooked function is being called, try this below the function definition:
var_dump( $order_id );
var_dump( $old_status );
var_dump( $new_status );
wp_die( var_dump( order ) );
Do you get any output? (remember to set this in wp-config.php):
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
Hi,
You are going to have to explain in further detail what it is you are trying to do. With this description it’s impossible to even begin to explain where to troubleshoot.
Thanks for the reply, @hube2 ! I have managed to use the ACF Javascript Library and I found it straightforward after a playing around for a bit.
Yes, that’s what I loved about these forums, the wealth of shared information of devs trying to solve a common problem! 🙂
Thank you @christian-denatorange-fr I am slowly getting into it and understanding its purpose a lot better. Merci! 🙂
@hube2 You’re the man, my friend, appreciate your feedback.
+1 on this. I know my way inside out of PHP, but I’ve always found JavaSript a bit of a pain. It would be nice to have some guidance for a simple example in the Javascript ACF documentation.
What do we need to do to get the acf JS object? When I type acf on the console or reference it on my code I get
Uncaught ReferenceError: acf is not defined
@hube2 makes total sense to me.
@24prod I have been using ACF-Builder and it works incredibly well, you might want to give it a shot:
https://github.com/StoutLogic/acf-builder/
Hi there!
If I understand correctly, you want to filter all tracks that have a certain condition already defined on the select.
I did something similar using: acf/fields/post_object/query (https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/).
You need to a filter that will call the function that will narrow down the choices using a query.
In my case (this is OOP code but you should get the gist), I added the filter:
$action_filter_register->add_filter( 'acf/fields/post_object/query/name=client_match_object_field', Match::class, 'filter_participant_options', 10, 3 );
And then defined the function that did the trick:
public function filter_participant_options( $args, $field, $post_id ) {
$allowed_states = EntityStates::get_allowed_states_for_matching();
$args['numberposts'] = -1;
$args['meta_query'] = array(
array(
'key' => '_state',
'value' => implode( ',', array_keys( $allowed_states ) ),
'compare' => 'IN',
),
);
return $args;
}
Hope this helps!
Bumping this one as I am completely baffled and it’s really annoying me. Essentially I hook up to after_setup_theme. At this stage I do not know the post ID other than by using $_GET, so this is my code:
$acf_loader = new ACFLoader();
$post_id = $_GET['post'];
$post_type = get_post_type( $post_id );
call_user_func( array( $acf_loader, 'create_' . $post_type . '_fields' ) );
When I do that, all of ACF fields functionality goes out the window (see movie above). If I do this, it works fine:
$acf_loader = new ACFLoader();
$acf_loader->create_volunteer_fields();
$acf_loader->create_client_fields();
$acf_loader->create_match_fields();
$acf_loader->create_area_coordinator_fields();
$acf_loader->create_referrer_fields();
This class merely uses FieldsBuilder to create the fields that are needed.
If I hook up to ‘wp’ or anything else, I am able to query the post but it’s too late for the ACF fields to take effect.
Any pointers appreciated! 🥺
No worries – what type of field is ‘related_brand’, exactly?
Here is the movie as I can no longer edit my post:
https://drive.google.com/open?id=1dvwDV2QI4xxdYykCLcCR-6_ShZEPiUPK
It’s a bit more complex than that. You would need to hook up the a filter that will allow you modify the values that the relationship field has:
https://www.advancedcustomfields.com/resources/acf-prepare_field/
Once you are in there, you create a query that will load only those posts that have the car tag and then return the value. When the post loads, it will only display those posts.
It depends on how your code is setup and names of variables, so it’s going to be a simple copy/paste.
Ok, so you want to get all posts type competitions that share the same brand as the current post? If so, you would need a meta_query:
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
$query = new WP_Query(array(
'post_type' => 'competitions',
'posts_per_page' => 5,
'post_status' => 'publish',
'meta_key' => 'related_brand',
'meta_value' => get_field('related_brand') // This is related brand of current post.
));
See if the query returns anything back?
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.