Great! Yes it is confusing at the beginning but with working through it you get a good understanding of how things work – same was it for me. Yes indeed – krazyplugin have a superb support don´t be afraid to reach out to them. They helped me a lot and were very patient even with stupid beginner questions 🙂 Enjoy!
Scrap that last question about whether I should be using acf Dokan on its own. I just deactivated the standard advanced custom fields plugin and can see acf Dokan can’t function without it.
I’ve looked if there’s another plugin acf Dokan with “pro” on the end as you described but it only brings up acf Dokan so I assume I have the right software. I have Dokan pro but I haven’t activated the key as i’m on a temporary domain at the moment.
I’m totally confused, as yours works but on other threads I could see someone was given advice to place some js somewhere but it was a bit vague for me to understand.
No worries, thanks for taking the time to look. It turned out my main issue was converting the array to a string, in case this is ever useful to anyone else… this worked
<?php
echo '<div class="row mb-5"><div class="col-12"><h3>History</h3></div>';
foreach($madein as $term)
{
$a = get_field('podcast_topic', 'shows_'.$term->term_id); // get the field array
$topicforuse = json_encode($a); // use json to convert to a string
if (strpos($topicforuse, 'history') !== false) { //check the string for a keyword
echo '<div class="col-6 col-md-3 col-xl-2 p-2"><a href="' . get_term_link( $term ) . '">';
echo wp_get_attachment_image( get_field('podcast_category_thumb', $term), 'thumbnail', false, array('class' => 'img-responsive rounded','style' => 'width:100%;height: auto; aspect-ratio: 1 / 1') );
echo '</a></div>';
}}
echo '</div>';
?>
Kind of, the taxonomies are:
Shows
Country
Topics
Under shows I can assign a topic, and the country (amongst other fields) via ACF. The code I added at the top allows me to display a list of Show Taxonomy items based on the country field (podcast_country) – it’s a single item so I used a text field and the query was quite easy to create.
What I’m trying to do is list the Shows based on the ‘podcast_topic’ field, but because that’s a taxonomy it outputs as an array and I can’t figure out how to get it to work.
The answer is, not easily.
ACF will store the values related to each post.
There are various actions and filters. For example acf/save_post and acf/update_value.
You could use these actions and filters to store data in a custom table by creating custom queries using $wpdb.
I am not an ACF developer. Just the guy that patrols these forums. The developers don’t monitor these forums. To submit a feature request you need to contact them directly.
When I duplicate a field group on multiple sites I do this by either creating the field group in the theme in the acf-json folder or exporting and importing the field group, or even building common field groups into a plugin.
When I do imports where I will not have this standardization then I use WP All Import Pro with the ACF add on that deals with updating the fields properly.
I realize that it is too late for your current situation. But if you will have standardized fields on multiple sites then these should be done so that they are the same field names and keys. But I would look into a tool like WP all import that will do this rather than trying to roll your own.
BTW, I will have the post id because i am doing wp_insert_post which returns the newly created post.
The data I am currently using for testing with acf_maybe_get_field is data that was already imported, but not imported using the field_key. It was just using the wp_insert_post and the field names. This data when I use get_fields() returns null.
In my new importer, I’m going to do like the example shows on the link you provided. I need to test this, but i was thinking something like this:
function helper_get_field_key($field_name, $post_id){
$maybe_field_obj = acf_maybe_get_field( $field_name, $post_id, false );
return $maybe_field_obj['key'];
}
// Create new post.
$post_data = array(
'post_title' => 'My post',
'post_type' => 'my-post-type',
'post_status' => 'publish'
);
$post_id = wp_insert_post( $post_data );
// Save a basic text value.
$get_field_key_email = helper_get_field_key('email', $post_id);
$value = "[email protected]";
update_field( $get_field_key_email, $value, $post_id );
I think something like this will work, i haven’t tested this exact scenario yet. I will today though and i can report back.
If you are trying to show posts that have both a spcific “podcast_country” and a specific “podcast_topic” then you need to query posts instead of trying to list terms.
Because you have a repeater inside of a repeater. A group field is a repeater that always has a single row. You cannot refer to the repeater with the name “group_main_info_phones” to update the field.
Like I said, you need to use field keys
$value = array(
// field_XXXXXXX is the field key of your nested repeater
// it's value is an array of rows for the repeater
'field_XXXXXXX' => array(
// each row of the repeater is a nested array
// that holds field key => value pairs
array(
// field_YYYYYYY is the field key of "name" field
'field_YYYYYYY' => 'Jon',
// field_ZZZZZZZ is the field key of "phone" field
'field_YYYYYYY' => '0897654007'
)
)
);
// field_AAAAAAA is the field key of the group field
update_field('field_AAAAAAA', $value, $the_record_id);
Dear, John Huebner!
Why does it work like this?
Structure:
1. repeater – repeater_test
$post_data = array(
'post_title' => $data["main_info"]["company"],
'post_content' => "",
'post_status' => 'publish',
'post_author' => get_current_user_id(),
"post_type" => "companies"
);
$the_record_id = wp_insert_post( $post_data );
add_row("repeater_test", ["test"=>"adding to the row should"], $the_record_id);
But it doesn’t work like this
Structure
1. field group – (group_main_info)
1.2 repeater – (phones)
add_row("group_main_info_phones", ["name"=>"Jon","phone"=>"0897654007"], $the_record_id);
I tried to write element keys and it doesn’t work the same way.
I just tested acf_maybe_get_field() on a field that exists but was never saved. The function returns false. I have no idea how you are getting the field key by using that function if the field has no value.
Under these conditions this line should be causing a PHP Undefined Index Warning:
return $maybe_field_obj['key'];
There is no reliable way that I know of using just the field name to get a field key for a field that does not have a value for a specific post.
You could potentially use acf_maybe_get_field() if you supplied a post ID for a post that you know will have the value set. From what I’m hearing there would be no way to know what post ID to use.
You could possibly do a query to get a post where the field key references already exist. Let’s assume that you know that if the field “email” exists for a post and has a field key reference in the db that all other fields will also exist.
$args = array(
'post_type' => 'your-post_type',
'posts_per_page' => 1,
'no_found_rows' => true,
'meta_query' => array(
array(
'key' => '_email'
'compare' => 'EXISTS'
}
)
);
$test_query = new WP_Query($args);
if (!empty($test_query->posts) {
$valid_fields_post_id = $test_query->posts[0]->ID;
}
But this will not work unless you can be sure all of the field references exist. You could also check for the existence of all field key references but I think if you have too many fields you need to check that the query could time out.
I’m having the same problem. I have created 2 rules for an advance custom field to only appear on products and those that equal a specific category. Like yourself, it functions as expected with woocommerce for the admin side but not with dokan for the seller upload form. The field is appearing despite my conditions set.
I only want this field group to show on Dokans seller product upload if they’ve selected a particular category.
I’m quite new to all this so assume you’re talking to simpliest person lol. Any help gratefully received
Dang I was so close, thank you! That seems to solve the error I was getting and it’s displaying the checkbox labels now, but it’s also displaying most of the classes as “unavailable” when they should be “available”.
This one shows only one class as “available” when it should be all 7.
<?php
$tier = get_field('tier');
$values = $tier['available_features'];
$field = get_field_object('tier_available_features');
$choices = $field['choices'];
?>
<?php foreach($choices as $choice => $label) : ?>
<?php foreach ( $values as $value ) : ?>
<?php if( $value['value'] == $choice ) {
$status = 'available';
}
else {
$status = 'unavailable';
} ?>
<?php endforeach; ?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
This version lists the checkbox labels correctly but marks them all with the “unavailable” class.
<?php
$tier = get_field('tier');
$values = $tier['available_features'];
$field = get_field_object('tier_available_features');
$choices = $field['choices'];
?>
<?php foreach($choices as $value => $label) : ?>
<?php if (in_array($value, $values)) {
$status = 'available';
}
else {
$status = 'unavailable';}?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
What am I missing here to get it to appropriately return the value of the checkbox?
Hello:
There are several fields built into the ACF oEmbed dialog. Those would include “Required, Embed Size (Width, Height), Wrapper attributes” I am sure there are more. That is what I remember off the top of my head.
I was hoping to be able to grab the Embed Size if it is set by the data entry person.
Thanks!
BC
Actually, no.
This function like all others requires that the field key reference exists in the database for the current $post_id. It uses a combination of the post ID and field name to look up the field key reference.
It all ends with the function acf_get_metadata(). In this function you will find
$meta = get_metadata( $type, $id, "{$prefix}{$name}", false );
$prefix in this case in an underscore (_), so it is trying to get the meta value of the meta key (your field name) prefixed with “_”. This is where the field key reference is stored. This meta value will only be set if the post already has a value for the field.
As stated in the update_field() document
Updating via field key
This example shows how to achieve the same result as above using the field’s key instead of its name. The field’s key should be used when saving a new value to a post (when no value exists). This helps ACF create the correct ‘reference’ between the value and the field’s settings.
The reason this is it because field names can be duplicated. All ACF operations actually need the field key to work. Field keys are unique. When you save a new post in the admin every input name is the field key, not the field name. ACF really has no way of knowing what field you are referencing by a field name and the field name has no meaning without the reference, which is why a field key reference is inserted into the DB for every field value that is saved for every post.
I have been trying to get this code to work but I keep getting an error “Trying to access array offset on value of type bool on line 110”. My checkbox field name is available_features and it is a sub field in a group name tier. I have tried both codes:
Version 1:
<?php
$tier = get_field('tier');
$values = get_field($tier['available_features']);
$field = get_field_object($tier['available_features']);
$choices = $field['choices']; // This is the line 110 that keeps throwing the error ?>
<?php foreach($choices as $choice => $label) : ?>
<?php foreach ( $values as $value ) : ?>
<?php if( $value['value'] == $choice ) {
$status = 'available';
}
else {
$status = 'unavailable';
} ?>
<?php endforeach; ?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
Version 2:
<?php
$tier = get_field('tier');
$values = get_field('available_features'); // This is the difference in V1 and V2
$field = get_field_object('available_features'); // This is the difference in V1 and V2
$choices = $field['choices']; // This is the line 110 that keeps throwing the error ?>
<?php foreach($choices as $choice => $label) : ?>
<?php foreach ( $values as $value ) : ?>
<?php if( $value['value'] == $choice ) {
$status = 'available';
}
else {
$status = 'unavailable';
} ?>
<?php endforeach; ?>
<li class="<?php echo $status;?>"><?php echo $label;?></li>
<?php endforeach;?>
I have also tried both with “get_sub_field” and “get_sub_field_object”. I keep running into the same error.
I like the helper function here, but what if I am not registering the field groups?
is there another similar function to get the key value of the field?
I use wp_insert_post to import data into wordpress, should i use a different function. I have the same issue as you. get_fields() is empty until i manually save my post.
I don’t want to run into this issue anymore. Let me know your suggestions.
I may answered my own question here. I believe I can write a function where I pass it the name of the field and then use this get_field_objects to get the key. Thoughts?
I appreciate that direction, it has given me new angles to Google.
I’ve found the following:
https://github.com/AdvancedCustomFields/acf/issues/453
https://www.implenton.com/alpine-js-directives-and-wordpress-sanitization/
Nothing quite working just yet. But a new batch of Google Search tabs is still progress.
I once covered a way that I did events with multiple dates. https://support.advancedcustomfields.com/forums/topic/helping-planning-acf-project/
I don’t think that creating another post while creating a post is something that is required and personally it is not something that I would ever supply to a client. They must understand that the related post must exist before they can select it in a relationship field.
Hi Tony,
Thanks for the responses! The shortcode idea is a good one and works nicely, and I’ll use it in the future when I need to quickly access ACF data from an HTML template.
I ended up just making new ACF blocks for the content that I wanted to display, and then inserting those blocks into the HTML templates. If you or anyone else stumbling on this doesn’t know, ACF has a handy register block function that can be used to quickly register a block without going through the endorsed WordPress route (which uses JSX and has separate Save and Edit functions for rendering).
I registered a person block, for instance, and then could add it to my HTML template with the following code:
<!-- wp:acf/person {"id":"block_627aa5c420a3d","name":"acf/person"} /-->
Then I can supply that block a good old fashioned PHP template during registration, allowing me to fully access the ACF field data from the render template and to further dictate how that block is displayed in the HTML template.
Like you said, this solution doesn’t seem elegant, nor is it efficient if you need to do it for many different types of posts.
You need to supply the term. See Adding fields to a taxonomy
$queried_object = get_queried_object();
?>
<a href="<?php bloginfo('url'); ?>/<?php the_field('page_link', $queried_object); ?>"><?php the_field('page_name', $queried_object); ?></a>
Hi, ReHo20. Thanks for those pointers. After posting this question I found that I could pass a post_id
, or more specifically a **user id** to the acf_add_options_sub_page()
function which enabled me to effectively have an options page which works for editing user meta. The steps I used were:
– Sending the user to admin.php?page=crm-client&user=2 via a custom table of users on my plugin landing page in admin
– Creating an options page with acf_add_options_sub_page()
with post_id
set to $_GET
the user_id from the URL
– I set the position
to 99 and then hid that menu item with CSS so that my client wouldn’t find themselves on the Client Settings page without an id in the URL, i.e.: they can only get to it via the user table
And it works like a charm! This was always a more desirable solution than hacking the default WP user page in admin as it gives me much more control and means that we still have that more detailed user options page at our disposal.
Thanks again for taking the time to share your solution.
Hi John,
this was written quite a while ago and it looks like that plugin hasn’t been developed in a long time. Is there a new solution to this?
Thanks
Daniel
This is my solution for this:
Add this to execute a particular script:
add_action('acf/input/admin_footer', array($this, 'filter_results'));
public function acf_filter_products_selection() {
?>
<script type="text/javascript">
(function($) {
acf.add_action('ready', function( $el ){
acf.add_filter('select2_ajax_results', function( json, params, instance ){
//Do whatever you want with the json.results array that contains the
//results from the DB
return json;
});
});
})(jQuery);
</script>
<?php
}
I guess I’m missing something as this now returns nothing:
$builder_args = array(
'post_type' => 'builders',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
'lot_clause' => array(
'key' => 'builder_lot',
'value' => 'meta_value',
'compare' => 'EXISTS',
),
'title_clause' => array(
'key' => 'builder_title',
'value' => 'meta_value',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'lot_clause' => 'ASC',
'title_clause' => 'ASC',
),
);
I’m not sure if key=value parts of the clauses are correct.
The link you provided says: “Note that a ‘meta_key=keyname‘ must also be present in the query.”
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.