You are using a filter to alter the choices of the field and not the value of the field. This makes your question confusing.
Also, since this is some type of selection field, it is likely that the value returned by get_post_meta
is an array, but this is a guess. Some select type fields store array values.
I do this for posts_per_page for different post types so that clients can determine how many posts to show on archive pages.
You need to create a pre_get_posts query and check settings and alter the query based on the settings.
Here is an example filter I created for a recent project that does this
function pre_get_posts_locations($query) {
// not in admin or if not main query
if (is_admin() || !$query->is_main_query()) {
return;
}
// only queries for location post type
if (!isset($query->query_vars) ||
!isset($query->query_vars['post_type']) ||
$query->query_vars['post_type'] != 'location') {
return;
}
// not for single
if (is_single()) {
return;
}
// set locations per page
$posts_per_page = get_field('locations_per_page', 'options');
if ($posts_per_page === NULL || $posts_per_page === false) {
// if never set show default of 10
$posts_per_page = 10;
}
if (!$posts_per_page) {
// if set to 0 show all
$posts_per_page = -1;
}
$query->set('posts_per_page', $posts_per_page);
// show only active locations
$meta_query = array(
array(
'key' => 'active',
'value' => 1
)
);
$query->set('meta_query', $meta_query);
$orderby = array(
'menu_order' => 'ASC',
'title' => 'ASC'
);
$query->set('orderby', $orderby);
}
The orderby part of the query at the end could also be set up to use options settings if needed.
having the same query , so following better.
OMG thank you @ireth92
I have been looking for this solution and now I find it in your post. I appreciate it!
By the way, in my case I had to add another dot to the button link
<a href="../page-edit-item/?post=<?php echo $postID; ?>">Edit Post</a>
Amazon is the most popular online shopping website. Its started a lot of sale and quizzes for the customers. Amazon Quiz is the best way to get fantastic gifts every day.
Hi! I need some guidance on how to output the selected values from a select on front.
I used this for renter and owner wonder(user_id==1) or renter(user_ID==0)
This is how its setup in ACF:
<img src="http://prnt.sc/v2thpx” alt=”ACF Setup” />
This is how I setup ACF
if( function_exists(‘acf_add_local_field_group’) ):
acf_add_local_field_group(array(
‘key’ => ‘group_5f8501f69e017’,
‘title’ => ‘Bizniness Rewards Programme’,
‘fields’ => array(
array(
‘key’ => ‘field_5f85021b748a4’,
‘label’ => ‘Owner Level (Booking & Service Fee)’,
‘name’ => ‘BZ-Owner-Level-Booking’,
‘type’ => ‘select’,
‘instructions’ => ‘Update Membership Package’,
‘required’ => 0,
‘conditional_logic’ => 0,
‘wrapper’ => array(
‘width’ => ”,
‘class’ => ”,
‘id’ => ”,
),
‘formula’ => ”,
‘readonly’ => 0,
‘choices’ => array(
10 => ‘Standard (10%)’,
‘9.5’ => ‘Bronze (9.5%)’,
9 => ‘Silver (9%)’,
‘8.5’ => ‘Gold (8.5%)’,
8 => ‘Diamond (8%)’,
‘7.5’ => ‘Opal (7.5%)’,
7 => ‘Ruby (7%)’,
),
‘default_value’ => false,
‘allow_null’ => 0,
‘multiple’ => 0,
‘ui’ => 1,
‘ajax’ => 0,
‘return_format’ => ‘value’,
‘placeholder’ => ”,
),
array(
‘key’ => ‘field_5f865bca62817’,
‘label’ => ‘Renter Level (Booking & Service Fee)’,
‘name’ => ‘BZ-Renter-Level-Booking’,
‘type’ => ‘select’,
‘instructions’ => ‘Update Membership Package’,
‘required’ => 0,
‘conditional_logic’ => 0,
‘wrapper’ => array(
‘width’ => ”,
‘class’ => ”,
‘id’ => ”,
),
‘formula’ => ”,
‘readonly’ => 0,
‘choices’ => array(
15 => ‘Standard (15%)’,
14 => ‘Bronze (14%)’,
13 => ‘Silver (13%)’,
12 => ‘Gold (12%)’,
11 => ‘Diamond (11%)’,
10 => ‘Opal (10%)’,
9 => ‘Ruby (9%)’,
),
‘default_value’ => false,
‘allow_null’ => 0,
‘multiple’ => 0,
‘ui’ => 1,
‘ajax’ => 0,
‘return_format’ => ‘value’,
‘placeholder’ => ”,
),
array(
‘key’ => ‘field_5f890f5954ffa’,
‘label’ => ‘Earned Points’,
‘name’ => ‘earned_points’,
‘type’ => ‘number’,
‘instructions’ => ”,
‘required’ => 0,
‘conditional_logic’ => 0,
‘wrapper’ => array(
‘width’ => ”,
‘class’ => ”,
‘id’ => ”,
),
‘default_value’ => 0,
‘placeholder’ => ”,
‘prepend’ => ”,
‘append’ => ”,
‘min’ => ”,
‘max’ => ”,
‘step’ => ”,
),
),
‘location’ => array(
array(
array(
‘param’ => ‘user_form’,
‘operator’ => ‘==’,
‘value’ => ‘edit’,
),
),
),
‘menu_order’ => 0,
‘position’ => ‘acf_after_title’,
‘style’ => ‘default’,
‘label_placement’ => ‘top’,
‘instruction_placement’ => ‘label’,
‘hide_on_screen’ => ”,
‘active’ => true,
‘description’ => ”,
));
endif;
I want to display the selected value something like this
Hi, is there any update or news about repeater field ?
i was able to save normal fields to product variation but when i create repeater field it doesnt save the value, here is my code;
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
$choices['product_variation'] = 'Product Variation';
return $choices;
}
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $abcdefgh_i;
$abcdefgh_i = $loop;
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
$acf_field_groups = acf_get_field_groups();
foreach( $acf_field_groups as $acf_field_group ) {
foreach( $acf_field_group['location'] as $group_locations ) {
foreach( $group_locations as $rule ) {
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
function acf_prepare_field_update_field_name( $field ) {
global $abcdefgh_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$abcdefgh_i][", $field['name'] );
return $field;
}
add_action( 'woocommerce_save_product_variation', function( $variation_id, $i = -1 ) {
if ( ! empty( $_POST['acf'] ) && is_array( $_POST['acf'] ) && array_key_exists( $i, $_POST['acf'] ) && is_array( ( $fields = $_POST['acf'][ $i ] ) ) ) {
foreach ( $fields as $key => $val ) {
update_field( $key, $val, $variation_id );
}
}
}, 10, 2 );
function xxx_admin_head_post() {
global $post_type;
if ($post_type === 'product') {
wp_register_script( 'xxx-acf-variation', get_template_directory_uri() . '/inc/ss.js', array(
'jquery-core',
'jquery-ui-core'
), '1.1.0',
true ); // Custom scripts
wp_enqueue_script( 'xxx-acf-variation' ); // Enqueue it!
}
}
/* actions fired when adding/editing posts or pages */
/* admin_head-(hookname) */
add_action( 'admin_head-post.php', 'xxx_admin_head_post' );
add_action( 'admin_head-post-new.php', 'xxx_admin_head_post' );
… ‘acf/validate_field_group’ filter does not exist
This hook exists, but it is not documented, as is the acf/load_field_group hook.
The OP was asking how to hide entire field groups, or that is the impression I got, based on an option setting. acf/prepare_field is only useful for single fields and not entire groups. Using the hooks I mentioned you can alter the location setting for a field group to dynamically set on what post types it should appear.
You cannot have multiple fields with the same name used on the same WP object (post, term, user, etc). Fields used on the same object must have unique names.
Your question should be directed to elementor or someone that is versed in the use of elementor for page building https://docs.elementor.com/article/381-elementor-integration-with-acf
Okay, I asked the question and then immediately answered it myself. You can see in includes/fields/class-acf-field-taxonomy.php line 15 that the terms are saved to the post with a priority of 15. If you don’t specify the priority of your acf/save_post function, it defaults to 10, which will run before the terms save. The easy fix for me was the change my function priority to 20. Everything still works and now my terms are saving on new posts!
If the issue is only there when you’re using a feature supplied by the other plugin, then the issue is more than likely with the other plugin. In any case the place to ask is the other plugin.
In order for this to be answered here you have to hope that someone using ACF AND Elementor AND ACF Frontend Form for Elementor AND finds your topic and wants to look into why there is an issue with that specific setting. This is unlikely to happen on this forum.
Or you can try to contact the developer here https://www.advancedcustomfields.com/contact/
—————-
Se o problema ocorrer apenas quando você estiver usando um recurso fornecido por outro plug-in, é mais provável que o problema seja com o outro plug-in. Em qualquer caso, o lugar a ser perguntado é o outro plugin.
Para que isso seja respondido aqui, você tem que esperar que alguém usando ACF e Elementor e ACF Frontend Form para Elementor encontre seu tópico e queira descobrir por que há um problema com aquela configuração específica. É improvável que isso aconteça neste fórum.
Ou você pode tentar entrar em contato com o desenvolvedor aqui https://www.advancedcustomfields.com/contact/
Currently, I can only filter the Stores by the custom fields directly associated with the Stores post type.
This is always. In order to filter the store by the field of a product that field would also need to be stored associated with the store.
however… what I’m seeing is that you want to filter the products while showing the store and not the stores…..
// get the product ID values
$product_ids = get_field('products', false, false); // acf relationship
// by setting the 3rd parameter to false you get just a list of post IDs
if ($product_ids) {
// ensure that products is an array,
// and that the values are integers
if (!is_array($product_ids)) {
$product_ids = array($product_ids);
}
$product_ids = array_map('intval', $product_ids);
// do your own query to get the posts
// I only include the necessities here
$args = array(
'post_type' => 'product',
'post__in' => $product_ids,
'meta_query' => array(
array(
'key' => 'status'
'value' => $_GET['product_status']
)
)
);
$products = new WP_Query($args);
if ($products->have_posts()) {
while ($products->have_posts()) {
$products->the_post();
// output product data here
}
wp_reset_postdata();
}
} // end if products
Of course you’d also need to adjust the store meta query to skip the fields associated with the product.
Apologies if this comes as duplicate posts. I have posted twice but did not go through.
I got a white editor page after I added the second register block. I got the editor back when I removed it and I am not sure what’s wrong as written. I will appreciate some help.
function register_acf_block_types() {
// register a testimonial block.
acf_register_block_type(array(
'name' => 'testimonial',
'title' => __('Testimonial'),
'description' => __('A custom testimonial block.'),
'render_template' => 'template-parts/blocks/testimonial/testimonial.php',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'testimonial', 'quote' ),
));
// register a testimonial block.
acf_register_block_type(array(
'name' => 'landing_page_content',
'title' => __('Landing Page'),
'description' => __('A custom landing page block.'),
'render_template' => 'template-parts/blocks/landing/page_landing.php',
'category' => 'formatting',
'icon' => 'dashicons-format-aside',
'keywords' => array( 'landing', 'card' ),
));
}
// Check if function exists and hook into setup.
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'register_acf_block_types');
}
This is my code. 2276 is the block ID. When I make the changes on the block from the first page (homepage), then the changes are not passed to the second page (services). I wonder what I am doing wrong.
<?php
$block_ID=2276;
$services_header_title = get_field('services_header_title', $block_ID);
?>
<div class="col-lg-12">
<h2><?php echo $services_header_title; ?></h2>
</div>
<?php if( have_rows('services') ): ?>
<?php while( have_rows('services') ): the_row();
// Get sub field values.
$service_title = get_sub_field('service_title', $block_ID);
$service_content = get_sub_field('service_content', $block_ID);
?>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<h3><?php echo $service_title; ?></h3>
<p><?php echo $service_content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
I solved the problem myself using a variable and REGEX 😉
'meta_query' => array(
array(
'key' => 'birthday',
'compare' => 'REGEXP',
'value' => $year.'[0-9]{2}[0-9]{2}',
),
)
));
Hi, THIS is the answer i was looking for, midwestdev.
But, for some reason, im receiving all the parent values and all the child values in the second field. Is there a way to show only the child values of the selection made in the first field?
Maybe i have to put the jQuery/Javascript in a separated file? I put it in the header
Thanks
So far I have managed to get the closest to what I need with this:
<?php
$phototext = get_field('communityphototext', get_post_thumbnail_id());
$js_out = json_encode($phototext);
?>
<script>
var phototext = <?php echo $js_out; ?>;
var communityImages = document.querySelectorAll('img');
if( communityImages.length > 0 ){
communityImages.forEach(function(image){
image.outerHTML = '<div class="community-image">'+ phototext + image.outerHTML +'</div>';
});
}
</script>
I use PHP to get the ACF field and then send the variable to Javascript,
I then use JS to programmatically add the ACF field to images.
I then style it with CSS (not shown here)
The problems I am facing:
– I am not fluent in either PHP of JS and I am still learning, so this is as far as I have gotten.
– This code is applying/showing the returned ACF value to all images that appear on the page, and not just to images that have the ACF field ‘communityphototext’ filled with some text or the field “CommunityPhoto” set as true.
– The field I get using get_field('communityphototext', get_post_thumbnail_id());
seems to be the field of the main post image, so then it attributes the ACF value of the main post image to all images
$value = get_field('field_name', $attachment_id);
doesn’t seem to work to get an image ID, in php it returns nothing and when I pass it to JS it returns null.
Thank you once again for the attention and advice, It has been quite a brain burner for me but I feel like I have never been so close to achieving this
I’ve come to a conclusion that the issue is a polylang issue rather than ACF issue. If one is using only “post__in” and “post_type” in wp_query, it shouldn’t make a difference in which language the request is made in as we has the IDs. Alternative solution would be to change ACF Core class-acf-field-relationship.php to iterate over every saved ID and map its content to a new array with “get_post”. “get_posts” with “post__in” does not work but “get_post” with single ID works.
Did some further debugging, apparently the relationship fields results use wp_query with “post__in” parameters with proper IDs, hence I feel like this should work out of the box but for some reason stays hidden.
I’m actually facing the exact same issue! Also weird is that when loading the field, no “non-translatable” CPT posts are visible, but when doing AJAX request, they become visible. After saving, nothing seems to be selected but in post_meta I can see that a non-translatable post is selected.
You don’t seem to understand what @saltnpixels is trying to achieve. The result of the render_callback specified when registering a block through ACF is stored to database on save of the post.
The desired behaviour is to have the content generated on request. Thus the example of the latest three posts, which might be different each time a user views the post.
The solution to the problem is using the render_block
filter: https://developer.wordpress.org/reference/hooks/render_block
The second argument to this hook, $block
, is an array. You can compare $block['blockName']
against your block name (beware of the acf/
prefix) and return dynamic content when it matches.
$block['attrs']['data']
is an array of all custom fields you defined for the block.
Had the same query and stumbled on this page.
I found a solution that works perfectly for anyone else that ends up here.
1. Create a new ACF Field Group e.g. “Media Metadata”
2. Add a field as a Link or Page Link or URL (I used Page Link)
3. Then under Location => Rules “show this field group if” = Attachment
4. This will add a new link field into each media library item
(https://www.greengeeks.com/tutorials/article/add-custom-fields-to-media-wordpress/)
I used a Page Link and added links to my Media in the Media Library.
So when calling it in my Gallery loop code:
<?php
foreach( $images as $image ):
$link_id = get_post_meta( $image['id'], 'page_link', true); ?>
<a href="<?php the_permalink( $link_id ); ?>" title="<?php echo $image['alt']; ?>">
<img data-src="<?php echo $image['sizes'][$size]; ?>"/></a>
<?php
endforeach;
?>
OK thank you I actually got it to work like this… but is this proper usage? It seems to work well with using the get_field_object ?? Like this:
<div id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
<?php
$bullet_color = get_field_object('bullet_color');
if($bullet_color=="lite-color") {
echo $lite-color;
}elseif ($bullet_color=="dark-color") {
echo $dark-color;
}
?>
<?php if ( have_rows( 'list_item' ) ) : ?>
<ul class="<?php the_field('center');?>">
<?php while ( have_rows( 'list_item' ) ) : the_row();
// var
$selection = get_sub_field('bullet_style');
if($selection=="asterisk") {
echo $ion-asterisk;
}elseif ($selection=="checkmark") {
echo $ion-checkmark;
}
?>
<li class="<?php echo $selection?> <?php the_field('bullet_color'); ?>">
<?php the_sub_field( 'add_item' ); ?> </li>
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
</ul>
</div>
It seems that if I switch to the underscore it doesn’t echo out the class properly which actually is .ion-asterisk? I think it’s odd because the class does have a dash in it…
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.