What do you mean by
custom folder feature in a gallery
ACF does not have a custom folder feature.
The functions you are using do not have filters that are easily usable. You would have to manipulate the actual SQL. They will always return something based on the WP post date.
If you want this to work then you’d have to update the WP post date based on the ACF field value.
If you don’t want to do that then you’d have to write your own queries.
I would make the following suggestions.
You should also be limiting your queries for the first and last post to 1 as well, there is no need to get multiple posts when you only need 1.
For the JS to work you’ll need to enqueue after loading ACF.
wp_enqueue_script( 'date-picker-js', get_stylesheet_directory_uri() . '/js/custom_date_picker.js', array('acf-input'), '1.0.0', true );
Hi John,
Thanks for the guidance. Based on that, I went back to the People CPT, added Relationship fields, and linked them to the appropriate Review CPT fields. And with the customized GB filter for each instance, we’re seeing lists of Review titles on the People pages generated by the GB query loop. There are still some refinements to make when the Relationship field is blank. Fingers crossed that it will be something easily fixable. But in case, can you point me to any block-based plugins that support both use cases “out of the box” so to speak?
I’ll make the case resolved though, with thanks for the clarifications that put me on the right course.
I don’t see any reason why this would not work
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'tas_album_order');
$query->set('order', 'DESC');
I can’t debug why you are getting the error without knowing the exact errror.
https://wordpress.org/documentation/article/debugging-in-wordpress/
The issue is being understood. You are showing fields on a WC product page. While you are using ACF to hold those fields the conditions involved and the code required is specific to Woo Commerce (WC), not ACF.
In your function, you need to get the product ID as outlined in the first link I provided: https://stackoverflow.com/questions/27385920/woocommerce-get-current-product-id
Then you need to use that product ID to get the categories that the product is in as outlined in the second link I provided: https://stackoverflow.com/questions/15303031/woocommerce-get-category-for-product-page
Then you need to loop over that list of categories to see if you product is in a given category and display the fields based on that condition.
I would suggest using bidirectional relationship fields. There is code available on this site as well as several plugins that offer this. The latest version of ACF also provides bidirectional support for relationship fields.
Queries for relationship fields without using bidirectional relationships are complex and I am unsure if you can to them using that filter. https://www.advancedcustomfields.com/resources/querying-relationship-fields/
If it helps, here’s the filter that is working for Use Case 1. The Query Loop itself is configured to point to the Review CPT. Then a custom class (in this case, btc-writers-roles-relationship) is added to the Grid element under that. The PHP filter script is called when it sees the custom class name, plus the relationship field in the current Reviews CPT post (in this case, writers), and enables the block to reference fields to return and format in the block UI. Can you suggest changes that would make this work in “reverse” … the Use Case 2 above?
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
// find the block with <code>btc-writers-relationship</code>
if (
! empty( $attributes['className'] ) &&
strpos( $attributes['className'], 'btc-writers-relationship' ) !== false &&
! is_admin()
) {
// get the relate post ID
$queried_object = get_queried_object () ;
$relationship = get_field( "writers", $queried_object );
// Merge the current $query_args with the new args
return array_merge( $query_args, array(
'post__in' => $relationship,
) );
}
return $query_args;
}, 10, 2 );
Hi John,
Thanks for the reply. I was just about to come back here and note that, indeed, the issue was a collision in similarly named taxonomies (early try, but inappropriate) and the relationship fields. Once I cleared that up, it’s working as expected.
If I may, one followup question?
Use Case 1: A query loop for a CPT1 Reviews content layout is pointing to CPT2 People. And for each of the three relationship fields, there’s a PHP function to query the relationship field and return data (a list of People post titles). Now working.
Use Case 2: I should be able to leverage the same technique? That is, a content layout for CPT2 People a query loop that points to CPT2 Reviews. And for each of the three relationship fields, a PHP function can query the same relationship fields and return data (a list of Review post titles). The same PHP functions in Use Case 1 don’t appear to work. A specific PHP script also didn’t work.
In the ACF relationship field I’ve made sure “Filter by Post Type” is set to both CPT2.
Anything else that might affect this? (I’m assuming that this is not a use case for bi-directional relationships, which appears to be more about enabling data changes to flow back and forth.)
I’m really just asking if the theory of leverage the relationship fields in CPT1 Reviews is correct here and it’s just a matter of tweaking the PHP until the return in a CPT2 People is accomplished.
Thanks again!
Short answer: not really, no.
Long answer:
Option 1:
You would probably also need to have them input a label for the custom choice. You could create both an acf/load_value and an acf/prepare_field filter. In the acf/load_value filter you would set the field value of custom to the value of the image field. In the acf/prepare_field filter you would include the new value as one of the choices. Both would be required to ensure that the value is set and remains selected. You would also need to keep the “custom” value so that they could change it.
Option 2:
Create an acf/load_value field for the select field. In this filter check the value and then return the image url from the image field if set to custom.
Option 3:
use the function acf_update_field() which is not documented, to permanently change the selections of the field. This will only work if you are not using acf-json. Basically you create an acf/save_post filter. In this filter you use get_field_object() to get the field and you update the choices then call acf_update_field() with the changed field object. It is also possible at this point it would probably also be possible to force the re-saving of a json file but at the moment I’m writing this I don’t know what function would be used or how to call it.
The only reason I would do one of these is if you cannot do this by simply editing the PHP template.
You have something interfering with ACF and this is difficult to debug in a forum.
If you remove the “GeneratePress Query Loop and their specialized filter code” does the issue resolve?
If the answer is yes then that’s where you need to look.
If not then you need to deactivate other plugins and maybe try a different theme until you figure out where the conflict is.
The answer is no. Without the field key reference not only does ACF not know what type of field it is but ACF will not return the field if the field key reference in the DB is missing when using one of the ACF template functions. The latter part was added for security to prevent the use of fields created outside of ACF.
The issue and reason that the field key reference is needed is do to the simplicity of the WP meta tables. There are only 3 usable columns: post_id, meta_key, meta_value. There simply isn’t a way to relate a field to this data without the additional value.
This is also due to the fact that field names do not need to be unique. You could easily have two fields used on two different post types that are different types of fields with the same name. But the field key is always unique. ACF uses a combination of the post ID and the field name to get the field key reference used for the specific field name used on the specific post.
It is possible to bypass the use of the field key reference by using the field key in ACF template functions instead of the field name. Using the field name is provided as a convenience to developers to make it easier for coding and for code clarity. It is easier to use something like “background_color” and it is easier to see what that field is for instead of something like “field_XXXXXXX”.
use some more smart way to find the field type
ACF was built to work with what is provided by WP, it is basically a big wrapper for all of the meta and option functions in WP and does nothing that could not be accomplished without it. What ACF does is that it makes the building out of complex admin UIs simple and fast saving the developer the need to code it all themselves. You can even replace all of the ACF template function calls with native WP function calls, provided you also want to do all of the field value formatting yourself.
I’m sure that if anyone came up with something simpler that also worked within what is provided natively in WP that the developers would love to hear about it.
The feature request forum here was created many years ago by the original developer when he used to actually look at the forum on a regular basis.
Developers of this plugin do not monitor this forum any more, but the original forums are still here. The only people that visit and answer questions here now are other users.
It has indeed, without any working solution I can find, and I’ve waded through all the research many times.
What’s the difference between ‘Feature Requests’ and ‘Feedback’ ?
I think you were right, when you said there was a solution that was abandoned, that building on that work may be the way forward. It’s not that easy to find the source any more, it’s been removed from github.
You can try requesting it here https://www.advancedcustomfields.com/feedback/
This topic has come up many times in the past and multiple solutions have been provided [forum search results]
Thank you John,
I have simplified my code to the one below. However, upon form submit the WordPress site crashes. Is there anything wrong with this code?
<?php
acf_form_head();
get_header();
?>
<div id="content">
<div class="wrap">
<form id='post' class='acf-form' action='' method='post'>
<?php
$field_groups = array('group_596925d98777d');
acf_form([
'field_groups' => $field_groups,
'post_id' => 'new_post',
'new_post' => array(
'post_category' => 'academy',
'post_status' => 'draft',
'post_title' => $post_id,
),
'post_content' => false,
'form' => false,
'submit_value' => __("Submit post", 'acf'),
'updated_message' => __("Thank you!", 'acf'),
'html_updated_message' => '<div id="message" class="updated" style="display:block;"><p>%s</p></div>'
]);
?>
</form>
</div>
</div>
<?php get_footer();
I also tried this approach, but this crashed the site with a fatal error:
function tas_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'post' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post' ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'tas_album_order');
$query->set('order', 'DESC');
}
// return
return $query;
}
add_action('pre_get_posts', 'tas_pre_get_posts');
Pods has features when adding post types and taxonomies that are not available in ACF, for example post type relationships. Replacing these features could be problematic and/or require the addition of custom code that Pods uses to accomplish these features. This may be the reason that ACF has not include it on top of the fact that most people were using CPT-UI.
I would first make sure that I’m not using any special features that would need to be replace. That means only using the features available when using register_post_type() and register_taxonomy().
Then you would have to delete all of the post types and taxonomies from Pods and then recreate them with ACF. Deleting a post type or taxonomy in most plugins does not usually delete the posts or the terms associated with them.
All this being said, this is not something I would do, I don’t, as a general rule, retrofit client sites, they are what they are.
If I did decide this was necessary I would most definitely not attempt it without making a complete backup of the site before I started.
Thanks to everyone for your input. I don’t think that the issue is being understood, so let me try to clarify. I have two categories: Ammo & Guns.
The first part of the code deals with fields related to Ammo, while the second part of the code is strictly for guns. The code below works well but it shows all the fields for both categories. So What I am looking to accomplish is as follows:
If category equals “Ammo” then
add_action( ‘woocommerce_single_product_summary’, ‘sbnai_display_acf_field_1’, 30 );
function sbnai_display_acf_field_1() {
echo ‘<b>Per Round Cost:</b> ‘ . get_field(‘per_round_cost’) . ‘<br />’;
echo ‘<b>UPC:</b> ‘ . get_field(‘upc’) . ‘<br />’;
echo ‘<b>Caliber:</b> ‘ . get_field(‘caliber’) . ‘<br />’;
echo ‘<b>Bullet Weight:</b> ‘ . get_field(‘bullet_weight’) . ‘<br />’;
echo ‘<b>Bullet Type:</b> ‘ . get_field(‘bullet_type’) . ‘<br />’;
echo ‘<b>Case Type:</b> ‘ . get_field(‘case_type’) . ‘<br />’;
echo ‘<b>Combined Reviews:</b> ‘ . get_field(‘combine_reviews’) . ‘<br />’;
}
Else if category equals “Guns” then
add_action( ‘woocommerce_single_product_summary’, ‘sbnai_display_acf_field_2’, 31 );
function sbnai_display_acf_field_2() {
echo ‘<b>Width:</b> ‘ . get_field(‘width’) . ‘<br />’;
echo ‘<b>Height:</b> ‘ . get_field(‘height’) . ‘<br />’;
echo ‘<b>Depth:</b> ‘ . get_field(‘depth’);
}
I do not do searches on sub fields of repeaters, in fact I go out of my way to not use repeaters when I have to use the data in searches.
When I must use a repeater for data that needs to be search then I find creative ways to not actually search the repeater. Here is a post I wrote years ago on the topic in the internet archive https://web.archive.org/web/20190814230622/https://acfextras.com/dont-query-repeaters/
Another way to that I have done something similar is the same way that “The Events Calendar” plugin manages repeating events. There is a hidden post that represents each event. When the main post is saved then hidden child posts are added and deleted as necessary. Searches are performed on the child posts, these are used to get the parent post.
Hi
Please use below code. i think it will work for you.
$id = get_the_ID();
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$post_id = ‘product_cat_’.$term_id;
echo get_field(‘per_round_cost’, $post_id);
echo get_field(‘upc’, $post_id);
echo get_field(‘caliber’, $post_id);
echo get_field(‘bullet_weight’, $post_id);
echo get_field(‘bullet_type’, $post_id);
echo get_field(‘case_type’, $post_id);
echo get_field(‘combine_reviews’, $post_id);
Here are all the screenshots:
https://drive.google.com/drive/folders/1XfojstMOTl_nCTYLqeGpvW3bDFxQ9B3C?usp=sharing
Hi John,
Thank you so much for your reply. But unfortunately it doesn’t work 🙁
https://drive.google.com/file/d/1YJrxKaEKjtQ0pJOxnG_0n33H6TgVKczg/view?usp=sharing
Did I miss something?
Heh, ignore that, I changed thoughts mid code and did not go back to remove it. I was thinking about a repeater when I started wich requires an nested array of rows. I replaced that with $row = array()
I would give up on updating the row and instead update the entire group field, the only question would be what other fields are in that group. This should work.
// this will hold the value to update the group with
$group_field_value = array();
// initialize row value just in case the loop returns noting
$row = array();
// loop group field and get existing values
if (have_rows('field_q3md98zvnm241', $post_id)) {
while (have_rows('field_q3md98zvnm241', $post_id)) {
// this loop always happens 1 time for a group field
// *********************************************************
// little known fact
// the_row() returns an array holding the row
// with field keys as indexes and unformatted field values
// *********************************************************
$row = the_row();
}
}
// set a new value for the "owner_name" field by field key
$row['field_4v8yw6n2hbpqs'] = 'get old value and insert here';
// update the field
// update the group field
update_field('field_q3md98zvnm241', $row, $post_id);
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.