I was just reviewing information presented here: https://craigsimpson.scot/performance-considerations-working-with-acf
We have caching at the server level enabled, but we haven’t been able to use any WordPress level caching, it also interferes with some functionality or another. The site is a larger member-based system with a lot of dynamic features.
There are a lot of plugins which add to load but I can’t control them, so I’m trying to do everything I can to improve performance and code that I’ve built.
I’m quite happy to pay for someone to provide me a good practice to follow through. I’ve been testing looking at saving multiple fields into a single field and compare performance but I thought I’d reach out here to get some suggestions I might not think of or consider from my experienced developers.
You can’t use get_sub_field() outside of a have_rows() loop.
You can get the value of that sub field directly using get_post_meta().
For example, if your repeater is named “repeater” and the sub field you want to check is named “sub_field” and the row you want to check is the 2nd row then
$value = get_post_meta($post_id, 'repeater_1_sub_field', true);
indexes start a 0 so the second rows index is 1.
This is how repeaters and sub fields are named
"{$repeater_name}_{$index}_{$sub_field_name}"
So, I’m a little late here and you already figure it out.
The explanation is that when using update_field() you must supply the value as ACF stores the value. In this case you are using a user field and the value stored is an array of user IDs and not an array of user objects.
Most of the more advanced fields require you to know what ACF actually stores and like you discovered, you can figure out what ACF actually stores by getting the unformatted value by supplying the 3rd parameter in get_field(). Another way to do this is to search the database.
ahhh… silly mistake on my part… the “if” is missing the closing bracket… sorry about that.
if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) ) {
Hi Keith, I’m getting ‘Parse error: syntax error’ for the curly bracket at the end of this line:
if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) {
Nothing I have tried seems to fix the issue. Thank you!
Ahhh, I see.. I assumed the other way around because it’s a 1 to many from hospitals to branches…
I can’t think of the logic then.. hmmm…
The goal is to find “recommended” branches right? And a branch is considered “recommended” if a “recommended” hospital is attached to it? Am I right so far?
If so… and you are connecting them in the way that you are… choosing 1 hospital for each branch… then….
I would:
1) Create an array of all hospital IDs that *are* recommended. Hint: You can use WP_Query to return *just* the IDs.
Here’s the info from this link:
/// Return Fields Parameter
Set return values.
fields (string) – Which fields to return. All fields are returned by default. There are two other options:
‘ids’ – Return an array of post IDs.
‘id=>parent’ – Return an array of stdClass objects with ID and post_parent properties.
Passing anything else will return all fields (default) – an array of post objects ///
2) Then… Query branches that contain those hospital IDs… you can do a meta query such as:
'meta_query' => array(
array(
'key' => 'hospital',
'value' => array( 3, 4 ), // array of hospitals IDs from 1st query
'compare' => 'IN',
),
Cool?
Hi John, thanks a lot! I also tried this but no featured image is shown. I check the featured image with the following code:
<?php
// check if the post or page has a Featured Image assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
This is probably a stupid question. But I have defined $value in the functions.php with the following:
$value = get_field('1_bild', $post_id);
Is this correct? @John
searching titles and content is something that is part of WP and not something that is specific to ACF. Basically, ACF does a standard query with the ‘s’ parameter set. This can be altered with quite a bit of work, but I don’t know if you can limit it only to the query being done by the relationship field.
I found some code here for removing the content from search. https://nathaningram.com/restricting-wordpress-search-to-titles-only/
The problem is that I do not think there is anything that will identify the query as being for the relationship field.
Hi Keith,
Thank you very much for your quick help.
I put the following code in the message field:
<?php
global $my_row_number;
if ( !$my_row_number ) { $my_row_number = 0; }
$rows = get_field('allgemein_mitglied');
$specific_row = $rows[$my_row_number];
$sub_field_value = $specific_row['allgemein_profilbild'];
echo basename($sub_field_value['url']); // echos *just* the filename for the image
$my_row_number++;
?>
After updating, however, the field changes to a “one-line text field”. Unfortunately, the desired effect does not work.
I have a quick dirty solution for you, if I am understanding the issue correctly. Since this is just temporary, this should be an OK solution for this scenario.
Now… I am assuming that:
1) You have a repeater of data
2) You don’t mind adding another field within that repeater to display the filename
Also… my brain could have overthought this issue from the beginning. There *may* be a super simple way to accomplish this. However, it only took me a few minutes, so.. here goes:
Step 1: Install the ACF Enhanced Message Field plugin, so that we can add a field to your repeater that allows for PHP code. Thanks Dreb Bits!
Step 2: Add the following code into your newly created Enhanced Message Field in your repeater:
<?php
global $my_row_number;
if ( !$my_row_number ) { $my_row_number = 0; }
$rows = get_field('repeater-name');
$specific_row = $rows[$my_row_number];
$sub_field_value = $specific_row['image-field-name'];
echo basename($sub_field_value['url']); // echos *just* the filename for the image
$my_row_number++;
?>
Sure, it’s a weird solution, but assuming I understand your issue correctly, it works like a charm 🙂
Take care to change the Repeater field name, as well as the Sub field name when using the above code.
Hope this works! Let me know.
Keith
It’s may be just a matter of specifying the post type in the Query.
It sounds like you know this, but remember… the below code naturally has values that need to be replaced:
a) ‘my_custom_post_type’ = the ‘slug’ of your post type (3 places… plus the function name twice if desired)
b) ‘date’ = the ‘name’ of your ACF Date field (1 place)
<?php
function mycustomposttype_pre_get_posts() {
if ( is_admin() ) { return $query; }
if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) {
$query->set( 'post_type', 'my_custom_post_type' );
$query->set( 'meta_key', 'date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
}
return $query;
}
add_action('pre_get_posts','mycustomposttype_pre_get_posts');
?>
You may need to adjust ‘orderby’. I got ‘num’ from the ACF docs about querying date fields.
Sure, I will cobble some code together below. May need some tweaks 🙂
$args = array(
'post_type' => 'hospital',
'posts_per_page' => 10, // you could use -1 for 'all'
'meta_query' => array(
array(
'key' => 'recommended',
'value' => TRUE,
'compare' => '=',
),
),
);
$the_query = new WP_Query( $args );
$all_branch_ids = array();
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$all_branch_ids = array_merge( $all_branch_ids, get_field('branch_id_field') ); // this *should* work (untested)
}
wp_reset_postdata();
}
// Essentially, with the above, we are looping through 'hospital' records and appending the array of "branch ids" (from the relationship field) to an array of "all" bracnch ids. This *could* potentially introduce duplicates, for which we address below.
Of course, we are only getting "branch ids" that are *recommended*, because the Hospital Query only returns recommended hospitals.
$all_branch_ids = array_unique($all_branch_ids); // remove duplicates
// Now, we can query branches using: post__in
$args = array(
'post_type' => 'branch_clinic',
'posts_per_page' => -1, // use -1 for 'all'
'post__in' => $all_branch_ids // has to be array like: array( 2, 5, 12, 14, 20 )
);
$the_query = new WP_Query( $args );
Note that empty arrays passed to ‘post__in’ can have undesired results, and there is a special note to be made about ‘sticky posts’ when using ‘post__in’
I encourage you read this entire page: https://codex.wordpress.org/Class_Reference/WP_Query
Hi there…
When you are creating a Date field, you can choose the “display” format, and the “return” format independently… there is a Custom option where you can put the mask you specified above.
For taxonomy… you can choose a return type of Object, rather than ID.
Then, to output the taxonomy name, you would do:
(assuming a ‘single’ choice field like a radio or select button)
<?php
$term = get_field('qicname');
if( $term ) {
echo $term->name;
}
?>
For a multi-choice taxonomy, you would create a loop as shown here:
https://www.advancedcustomfields.com/resources/taxonomy/
You can do a quick query like:
$year = date( 'Y' );
$query = new WP_Query( 'year=' . $year );
Or a date query if you want to do it the regular way:
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' )
),
),
);
$query = new WP_Query( $args );
More info here: https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
Hello…
Since you have the Book, you can get the Book ID, then you can query all chapters where CF_Book = Book ID
Here’s a resource to help with Querying posts with Custom Fields.
Let me know if that’s helpful for you!
Hello…
With WP_Query you can use the argument post__in to pass in an array of post IDs…
See here: https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
So… with a Post Object ACF filed, you might be returning IDs or Post Objects (default).
If returning Post Objects, you can use this code to get an array of IDs:
$post_objects = get_field('po');
$post_object_ids = array_map(function($o) { return $o->ID; }, $post_objects); // array of IDs to pass to: post__in in a WP_Query
Careful though about passing an empty array to post__in because it will return all results.
Hi Rich…
Since you want both comparisons to be true, you can include an “AND relation” within the query:
array(
'relation' => 'AND',
array(
'key' => 'offer_start_date',
'value' => $today,
'compare' => '<='
),
array(
'key' => 'offer_end_date',
'value' => $today,
'compare' => '>='
)
)
Also, here are more docs on date queries that may be useful.
Hi @kollinsayz
I’m not sure what you mean when you say “based on the already existing posts”
You could use the acf/load_value filter to pre-populate the value a field.
Here’s the PHP manual for getting the current date/time.
But remember that WordPress time stamps posts automatically, and you can use the Date Parameters of a WP_Query to show posts associated with a certain time and date period.
Wouldn’t you query the hospitals first then?
Example:
1) Get all Branch IDs of all hospitals, where Recommended = TRUE
2) Add all those IDs to an array
3) Remove duplicates from the array of Branch IDs
4) Query Branches with Post IDs in ( post__in ) your array
Am I understanding correctly?
No other plugins are installed.
This issue is getting stranger as time goes on. On my test environment (http://acftax.wpengine.com/?p=1), those taxonomy fields randomly started working. However, on my staging environment, they’re obviously still broken. I tried switching themes to Twentyseventeen, re-adding my custom fields and taxonomies, and adding the acf_form
snippet, and instead of “The results could not be loaded”, I got “No results found”
Are their arguments that might be required with register_taxonomy
for custom post type taxonomies to work correctly with ACF’s taxonomy field inside acf_form
?
Thanks for the help, changed few extra strings to make everything work as I want to. As for value ‘1’ or TRUE(both work fine) I misspelled and wrote show_main in the key instead of show-main, heh.
In case if someone will need this
<?php
$posts = get_posts(array(
'cat' => $current,
'post_type' => array( 'page' ),
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'show-main',
'value' => '1',
)
)
)
);
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
loop
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
If you need specific identifier, not random ID or index, you can simply hash value of one of repeater fields, which you consider unique enough. It can be used to generate e.g. FAQ item permalink hash, it will be persistent enough, if you will not change question text too often.
<?php if ( have_rows('faqs') ): ?>
<?php while ( have_rows('faqs') ) : the_row(); ?>
<a href="#<?php echo md5( get_sub_field('question') ); ?>">
<?php the_sub_field('question'); ?>
</a>
<?php the_sub_field('answer'); ?>
<?php endwhile; ?>
<?php endif; ?>
In Beaver Themer is a little wrench icon near the social icons when you go to edit them? Can you just remove the icons that you don’t have links for?
To answer your question, you would add the code example i gave where the icons output in your template, but i am no familiar with beaver themer so i dont know if that is possible in your case.
It doesn’t work that way, but let’s pretend it did. I have a Boolean (True / False) field called “Show Me” that says if “Show Me” is True, show the field. This means that if “Show Me” is ever switched to False, then it would get hidden… forever (until you dug unto the database, but that’s neither here nor there).
The Conditional Logic for each field is for showing or hiding THIS field based on input from other fields.
In a more real world example, I create a few different fields. One called “Background Color”. One called “Background Image”. And one called “Background Video”. Above those three a create a radio button field that is called “Background Type” with the options of color, image, and video. I then add conditional logic to the “Background Color” field that says if “Background Type” is equal to color, show this field. Then I do the same for the other two background fields. You are now hiding unnecessary fields in your editor UI.
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.