You cannot set a value to something like project_price&options=ASC
You will need to parse the value that is submitted.
I would suggest something like
orderby:project_price;order=ASC
$posts = array(
'posts_per_page' => -1,
'post_status' => 'publish',
'post_type' => 'renders',
'meta_query' => array(
'relation' => 'AND',
'prod_type' => array(
'key' => 'product_type',
'value' => $render_cat,
'compare' => 'LIKE',
),
),
'orderby' => 'meta_value',
);
$settings = explode(';', $_GET['feature']);
foreach ($settings as $setting) {
$parts = explode(':', $setting);
$posts[$parts[0]] = $parts[1];
}
Interestingly, get_source_field()
is in a different place in my version of ACF Pro – media.php
– and it’s a <em>private</em> function
(any way around that?)
1)
So, if I straight-up attempt to get the source field using that inside my WP upload pre-filter…
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
// Attempt to get the ACF field
$field = get_source_field();
// Begin testing rename - not the finished article
$file['name'] = 'and-everything-is-awesome-' . $file['name'];
return $file;
}
… then the Media uploader warns:
“Post-processing of the image likely failed because the server is busy or does not have enough resources. Uploading a smaller image may help. Suggested maximum size is 2500 pixels.”
2)
But, if I ostensibly copy the function code into my filter function…
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ) {
// 1. GET SOURCE FIELD, FROM MEDIA.PHP
$field = false;
// Search for field key within available data.
// Case 1) Media modal query.
if ( isset( $_POST['query']['_acfuploader'] ) ) {
$field_key = (string) $_POST['query']['_acfuploader'];
// Case 2) Media modal upload.
} elseif ( isset( $_POST['_acfuploader'] ) ) {
$field_key = (string) $_POST['_acfuploader'];
}
// Attempt to load field.
// Note the <code>acf_get_field()</code> function will return false if not found.
if ( isset( $field_key ) ) {
$field = acf_get_field( $field_key );
}
// 2. GET USER'S ID AND USERNAME
// TEST RENAME
$file['name'] = $user_id . 'and-everything-is-awesome-' . $file['name'];
return $file;
}
… it successfully gets the field key at that point, and therefore supports getting the field, too.
So, let me think, what logic should I be using here… ?
* If this is an ACF field with the specified key (since wp_handle_upload_prefilter
will otherwise fire on every upload),
* Get the User ID for the User in the current Edit page
* Get User object and then Username slug
* Use that to recompose the filename, but retain the original extension
If so, I’m now hitting a non-ACF snag actually obtaining the current user from user-edit.php
ie. the following and another method…
// 2. GET USER'S ID AND USERNAME
// If is current user's profile (profile.php)
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
$user_id = get_current_user_id();
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
// Otherwise something is wrong.
} else {
die( 'No user id defined.' );
}
… provoke “An error occurred in the upload. Please try again later.” in the upload window… maybe because, the browser is still on the same page, user_id
is not available at the point the “Select Image” modal has appeared?? Hmm…
You cannot do a between query using just the month and day on a date field. A date field stores values as ‘YYYYMMDD’. There is no way that I know of to query between to values that only include the month and day. You might be able to query these by using a REGEX value query, but this would not work to get values between two months.
The only way to do this would be to have a field that only contains just the month and day.
Do a user query to get all the users then use delete_field() using the repeater field name. This will delete the repeater and all sub fields.
Without seeing code and having details about the field this question cannot be answered.
I’m using select fields in the current version and all are working as expected.
Not doing reverse relationship queries was the reason that plugin was created.
While this would be nice, this is not something that ACF is actually validating or forcing. The protocol needing to be entered is something that the browser is doing. ACF uses <input type="url">
and this causes the browser to do validation. You can test this by creating a simple html page with a form and a url input.
ACF is only catching the error already produced and displaying it in the same way as other errors for other fields.
I generally use a text type field for URLs and do my own validation with an acf/validate_value filter so that I can allow clients to enter any valid href value.
function validate_text_as_href($valid, $value, $field, $input) {
if (!$valid) {
return $valid;
}
if (!empty($value)) {
if (!preg_match('%^(https?\://|ftp\://|/|#|mailto\:|sms\:|tel\:)%', $value)) {
$valid = 'Enter a Valid HREF Value';
}
}
return $valid;
}
field_609103a4249a4 is the unique field key for the field. Because the field name can be changed this is what ACF uses to identify the field. This value never changes. You can see these field keys when editing a field group. Top of the page => Page Options, check the box for field keys.
When using acf/save_post if you use a priority of > 10 for your action then you can get the values from the DB by using ACF functions rather than trying to look in $_POST.
good question. I don’t think that hook will work because you can’t change the file name, only add errors for custom validation.
I would use the wp_handle_upload_prefilter hook.
Take a look in the file …/plugins/advanced-custom-fields-pro/includes/api/api-helpers.php
There is a function in there named get_source_field() that shows how ACF gets the field associated with the upload. I would do the same thing to determine if the upload is for an ACF field, what field, and then alter the file name if it’s the right field.
@lgladdy
I use locally
Wordpress 5.8.1
ACF version 5.10.2
Advanced Custom Fields Multilingual 1.9.1
WPML Media 2.6.5
WPML Multilingual CMS 4.4.12
WPML String Translation 3.1.10
WPML Translation Management 2.10.8.
When I save my custom widget, the widget get the ID media_widget-1
but the saved options receive the ID widget_media_widget-12
Workflow:
<input class="widget-id" type="hidden" name="widget-id" value="media_widget-12">
<– maybe the ID should be 1 not 12Update
media_widget-1
:<input class="widget-id" type="hidden" name="widget-id" value="media_widget-1">
Hope this will help.
ACF has it’s own function and hooks
$errors = apply_filters( "acf/upload_prefilter/type={$field['type']}", $errors, $file, $field );
$errors = apply_filters( "acf/upload_prefilter/name={$field['_name']}", $errors, $file, $field );
$errors = apply_filters( "acf/upload_prefilter/key={$field['key']}", $errors, $file, $field );
$errors = apply_filters( 'acf/upload_prefilter', $errors, $file, $field );
Same as the wp hook but also passes the $field.
I found some information about altering the upload file name
https://developer.wordpress.org/reference/hooks/wp_handle_upload_prefilter/
Thanks. I wonder how I would ensure that only applies to the ACF field in question, rather than filters ALL WordPress file uploads?
(FYI – It’s on user-edit.php. I’d like to rename the file being uploaded through an Image field – specifically, by getting the ID and then username of the user currently being edited… which is proving to be the next challenge).
Given that that I already have ACF filters going to take the Image and change the destination directory (as per the other thread), I’m not certain how it should fit together.
I think it is related to this update from Aug 25th:
* Enhancement – Improved security by running all user-generated content through
wp_kses()
by default
It started working after I replaced the line 75 of class-acf-field-message.php from:
echo acf_esc_html( $m );
to
echo $m;
Is there a way to avoid this function from running on the message field?
Thanks!
I know this is a bit late but I just had this same issue I was trying to solve. I managed to get a working proof of concept by Frankensteining a number of different tutorials together. I have an ACF field on the custom post type page called ‘homepage_order’ that I can enter data in and if I go to the posts list page I can see that content in a custom column and update the content from the Quick Edit box of each post. I am not a proficient PHP coder so I’m sure there are glaring errors and my security checks are not all there (some of them are causing the update to fail) but hopefully this will help point you in the right direction. If anyone has any comments on how I can make this better, I would be most appreciative.
//Adds the custom column to the exhibits posts list page
add_filter( 'manage_exhibits_posts_columns', 'heard_filter_posts_columns' );
function heard_filter_posts_columns( $columns ) {
$columns['homepage_order'] = __( 'Current Exhibits Order', 'heard' );
return $columns;
}
//Gets the data from the custom field populated in the exhibits post and adds it to the column
add_action( 'manage_exhibits_posts_custom_column', 'heard_homepageorder_column', 10, 2);
function heard_homepageorder_column( $column, $post_id ) {
// Image column
if ( 'homepage_order' === $column ) {
echo get_post_meta( $post_id, 'homepage_order', true );
}
}
//Creates a fillable field in the Quick Edit box
add_action( 'quick_edit_custom_box', 'heard_custom_edit_box_pt', 10, 3 );
function heard_custom_edit_box_pt( $column, $post_id ){
if($column == 'homepage_order'){
$html .= '<fieldset class="inline-edit-col-right ">';
$html .= '<div class="inline-edit-group wp-clearfix">';
$html .= '<label class="alignleft" for="homepage_order">Current Exhibits Order</label>';
$html .= '<input type="text" name="homepage_order" id="homepage_order" value="" />';
$html .= '</div>';
$html .= '</fieldset>';
}
echo $html;
}
//Saves any updates to the current exhibit order field in the Quick Edit box
add_action( 'save_post_exhibits', 'heard_update_custom_quickedit_box' );
function heard_update_custom_quickedit_box() {
//There are currently limited security measures in place on this. The quick edit field won't update when I add all of them. Still working on this.
//Check user permissions- this works
if ( 'page' == $_POST['homepage_order'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
}
// save homepage_order updates in Exhibits- custom post type
if( isset( $_POST['homepage_order'] )) { // where homepage_order is defined in the <input name="homepage_order">
update_post_meta($_POST['post_ID'], 'homepage_order', $_POST['homepage_order']);
}
return; // finish the function call
}
I would use the field key variant, not the field name. When ACF runs hooks for sub fields the field name for the filter is generally the concatenated version of the field name "{$parent}_{$child}"
but not always.
In some cases even using the field key variant is not sufficient. In some cases I have had to use either to field type variant or even the all fields variant acf/upload_prefilter
and then look at the values in the $field array and figure out a way to match to the field I want to filter.
Anyway, I usually try the field key variant first and if that does not seem to work then I use one of the others and start trying to dissect what is being passed in $field in order to make it work.
acf/upload_prefilter/name=my_acf_upload_fieldname’ change my_acf_upload_fieldname to the name of your ACF field.
For my_acf_upload_fieldname
, how would you reference an Image field that is inside a Group? Does it matter?
Hey, that looks great!!!
The only thing is, that in the date picker field there is always the date “01.01.1970” , but in the custom field it saves the correct date. https://imgur.com/a/JXIIybQ
Dear All,
i need a big help, please.
i have try everything but nothing.
how i can insert a video display in this code? I have created in Hero field a ombed sub-field
@if(!empty($hero['image']))
<div class="banner-image-wrapper">
@if(!empty($hero['other_images']))
@php
$element_classes = "swiper-slide px-0";
@endphp
<div class="swiper-container swiper-container-full-width mb-8">
<div class="swiper-wrapper ">
<div class="{!! $element_classes !!}">
@pic($hero['image'],['class' => ' banner-image '])
</div>
@foreach($hero['other_images'] as $k => $v)
<div class="{!! $element_classes !!}">
@pic($v,['class' => ' banner-image '])
</div>
@endforeach
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
@else
@pic($hero['image'],['class' => ' banner-image '])
@endif
</div>
@else
<div class="banner-image-wrapper">
@pic(get_field('default_hero_image','options'),['class' => ' banner-image '])
</div>
@endif
thank you in advance
Like I said, you can’t alter the format ACF uses to save the date value because that would cause ACF to not be able to display the fields for editing.
What you need to do is to figure out a way, if it is possible at all, to filter the values when the theme gets/uses them to match the format that is needed there. This depends on how the theme or another plugin is getting the value.
For example, if the theme or plugin is using get_post_meta() to get the value you could possibly add a filter on the "get_{$meta_type}_metadata"
hook called in the WP function get_metadata_raw() to alter the value being returned.
add_action('wp_head', 'product_details_meta');
function product_details_meta() {
// get the queried object
$queried_object = get_queried_object();
// see if it's a post
if (!is_a($queried_object, 'WP_POst')) {
// not a post
return;
}
// set $post_id
$post_id = $queried_object->ID;
// check post type
if (get_post_type($post_id) != 'product') {
// not a product
return;
}
// use $post_id to get ACF field values
if (get_field('gtin', $post_id)) {
?><meta property="GTIN" content="<?php the_field('gtin', $post_id); ?>" /><?php
}
}
See Screenshot of the field form I added with ACF for the field _catcbll_btn_label
. It’s a plugin-specific field, but I added it as a custom field entry in the ACF plugin, so that I would have an form field to enter data in the product screen.
In the product post screen, I need to save this value a:1:{i:0;s:22:"Request Shipping Quote";}
in the custom field _catcbll_btn_label
to cause a plugin known as Custom Add to Cart Button Label and Link to display a shipping quote button. Yet the ACF or WP is not handling it correctly, as the _catcbll_btn_label
field shows [""]
when I refresh the product page after submitting the value a:1:{i:0;s:22:"Request Shipping Quote";}
. Can you suggest anything helpful?
Hi John,
Thanks so much for your time on this. I replaced the “// your code to output meta tags here” with the code above, and the following code was my block of text added to the functions.php of my child theme:
add_action('wp_head', 'product_details_meta');
function product_details_meta() {
// get the queried object
$queried_object = get_queried_object();
// see if it's a post
if (!is_a($queried_object, 'WP_POst')) {
// not a post
return;
}
// set $post_id
$post_id = $queried_object->ID;
// check post type
if (get_post_type($post_id) != 'product') {
// not a product
return;
}
// use $post_id to get ACF field values
$value = get_field('gtin', $post_id);
if (get_field('gtin', $post_id)) {
?>
<meta property="GTIN" content="<?php the_field(gtin', $post_id);?>" />
<?php
}
Sadly, it threw up a critical error on all pages of my site when I included this.
If you happen to see anything glaring that I mistakenly did with the code above, please let me know. Once again, thanks for your time and your help is greatly appreciated. Thanks again.
if (get_field('gtin', $post_id)) {
?><meta property="GTIN" content="<?php the_field(gtin', $post_id);?>" /><?php
}
I would create an action in your functions.php file.
add_action('wp_head', 'product_details_meta');
function product_details_meta() {
// get the queried object
$queried_object = get_queried_object();
// see if it's a post
if (!is_a($queried_object, 'WP_POst')) {
// not a post
return;
}
// set $post_id
$post_id = $queried_object->ID;
// check post type
if (get_post_type($post_id) != 'product') {
// not a product
return;
}
// use $post_id to get ACF field values
$value = get_field('your_field_name', $post_id);
// your code to output meta tags here
}
Ok, found the problem. I had to add the code in TWO placed within the theme template file….
This was my final working code – if anyone finds this in a future search
/* START position for Advanced Custom Fields Content -- aka vendor-links */
$buttons = get_fields();
if( $buttons ) {
echo
'<div class="button-strip et_pb_button_module_wrapper et_pb_button_3_tb_body_wrapper">';
foreach( $buttons as $but_name => $but_value ) {
if( !empty( $but_value ) ) {
$but_label = get_field_object($but_name);
echo
'<a class="vendor-link3 ', $but_name ,'-button" title="',$but_label['label'] ,'" href="', $but_value ,'" target="_blank">', $but_label['label'] ,'</a>';
}
}
echo
'</div>';
}
/* FINISH position for Advanced Custom Fields Content -- aka vendor-links */
I then used some CSS on “vendor-link3” and the button name (like amazon-button) to style them as buttons.
Thanks for the pointers John.
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.