Your code looks correct to do what you describe. In what way doesn’t it work?
What template is running the loop?
Are you sure the filter code is running? You could try placing a var_dump
inside the filter inside the second if
check and see if it shows on the page.
Thanks for the screencast of the issue – very helpful!
I noticed in the ACF field settings for the before_photo
field, you have the return value set to Image Array. With this setting, more than 1 value is returned meaning WordPress doesn’t know which value to place in the src
.
You can change the return type to Image URL and this should give the expected result.
Assuming the update_field()
call in your post works as expected (I haven’t looked into this) you should be able to loop the output from WordPress’s get_post_meta()
function.
Something like this:
function update_fields_for_id( $post_id ) {
$fields = get_post_meta( $post_id );
foreach ($fields as $field_name => $field_value) {
// Don't process fields with leading underscore
// which are marked as private for internal use only
if ( 0 !== strpos( $field_name, '_' ) ) {
update_field( $field_name, get_field( $field_name, $post_id ), $post_id );
}
}
}
Hi neur,
Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:
With all of that in mind, I think this should work:
$year = '2018';
// Build date range from year
$date_range = array( $year . '-01-01', $year . '-12-31' );
$posts2018_args = array(
'post_type' => 'events',
'posts_per_page' => -1,
// Sort by custom meta_key
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
// Filter by custom meta query
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_category',
'value' => 'Indoor Track and Field',
'compare' => '=',
),
array(
'key' => 'event_date',
'value' => $date_range,
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
$posts2018 = get_posts( $posts2018_args );
Hi neur,
Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:
date()
function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
With all of that in mind, I think this should work:
$year = '2018';
// Build date range from year
$date_range = array( $year . '-01-01', $year . '-12-31' );
$posts2018_args = array(
'post_type' => 'events',
'posts_per_page' => -1,
// Sort by custom meta_key
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
// Filter by custom meta query
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_category',
'value' => 'Indoor Track and Field',
'compare' => '=',
),
array(
'key' => 'event_date',
'value' => $date_range,
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
Hi neur,
Based on what you’ve posted, I think you’re going for something like the code below. I’ve changed a number of things:
date()
function isn’t used in that way. It converts an integer value of seconds into a string formatted date. If you already know the date, you can pass it as a string on its own like '2018-01-01'
BETWEEN
value for compare
to pass both dates at onceWith all of that in mind, I think this should work:
$year = '2018';
// Build date range from year
$date_range = array( $year . '-01-01', $year . '-12-31' );
$posts2018_args = array(
'post_type' => 'events',
'posts_per_page' => -1,
// Sort by custom meta_key
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
// Filter by custom meta query
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_category',
'value' => 'Indoor Track and Field',
'compare' => '=',
),
array(
'key' => 'event_date',
'value' => $date_range,
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
I haven’t personally tried filtering a message field, however your syntax looks slightly incorrect.
See this page: https://www.advancedcustomfields.com/resources/acf-load_value/
Specifically:
// acf/load_value/name={$field_name} - filter for a specific value load based on it's field name
add_filter('acf/load_value/name=my_select', 'my_acf_load_value', 10, 3);
// acf/load_value/key={$field_key} - filter for a specific field based on it's name
add_filter('acf/load_value/key=field_508a263b40457', 'my_acf_load_value', 10, 3);
You have name
in your code where you are passing a field key.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.