Hi there.
I had the same issue a while ago and didn’t found a proper and safe solutions without a heavy SQL query.
So I created a plugin using functions by ACF to remove unused metadata.
Create a database dump and give it a try:
https://de.wordpress.org/plugins/whatwedo-acf-cleaner/
Hope it helps not just me
Thank you so much @hube2! 🙂 I really appreciate your feedback and for sharing your code. Here is what I ended up using. I kept your naming convention for convenience.
class MY_PROJECT_NAME_acf_filters {
// variable for row index
private $my_field_name_index = 0;
public function __construct() {
// add a filter when preparing repeater
add_filter( 'acf/prepare_field/key=field_601cf9424f9da', array( $this, 'init_my_field_name_index' ), 10, 1 );
add_filter( 'acf/prepare_field/key=field_6023ad409d89d', array( $this, 'filter_my_sub_field_name' ), 10, 1 );
// add filter for ths sub field that you want to filter
add_filter( 'acf/prepare_field/key=field_602e8a8821e89', array( $this, 'filter_my_sub_sub_field_name' ), 10, 1 );
}
public function init_my_field_name_index( $field ) {
$this->my_field_name_index = 0;
return $field;
}
public function filter_my_sub_field_name( $field ) {
// after filtering increment the field index
$this->my_field_name_index++;
// return the field;
return $field;
}
public function filter_my_sub_sub_field_name( $field ) {
$field['choices'] = [];
$row_index = $this->my_field_name_index - 2;
$product_id = (int) get_user_meta( 2, 'add_products_' . $row_index . '_produkt', true );
if ( ! empty( $product_id ) ) {
$pewc_group = new WP_Query( [
'posts_per_page => ' -1,
'post_type' => 'pewc_group',
'post_parent' => $product_id,
'post_status' => 'publish'
] );
if ( $pewc_group->have_posts() ) {
while( $pewc_group->have_posts() ) { $pewc_group->the_post();
$field['choices'] = [];
$group_title = get_the_title();
$group_id = get_the_ID();
$pewc_field = new WP_Query( [
'posts_per_page => ' -1,
'post_type' => 'pewc_field',
'post_parent' => get_the_ID(),
'post_status' => 'publish',
'order' => 'ASC'
] );
if ( $pewc_field->have_posts() ) {
while( $pewc_field->have_posts() ) { $pewc_field->the_post();
$field_label = get_post_meta( get_the_ID(), 'field_label', true );
if ( ! empty( $field_label ) ) {
$field['choices'][get_the_ID()] = $field_label . ' (' . get_the_ID() . ')';
}
}
wp_reset_postdata();
}
}
wp_reset_postdata();
}
}
// return the field;
return $field;
}
}
new MY_PROJECT_NAME_acf_filters();
Hey,
Has there been any progress made on this filtering? I have a very similar request.
Thanks
This is just a weird problem.
A couple of bits of information here…I’ve been doing some digging.
I installed WP DEBUG and I am only seeing in the Queries tab the value being updated once. I know that it is running twice though. I think the UPDATE sql statement in the logs only once.
Our site does use AJAX, but it is not firing on page load and that is literally all I am doing is just loading the page. When I refresh, I can see the number go up by 2. Like I explained before, if i open up my database and query the post i am on, if i keep refreshing on that query, I will see it update the number twice.
I have read some blogs that are saying that single.php can fire twice before of revisions or something so i added this to functions file.
I also tried moving my function from the functions file to the single.php template. The number still updated twice.
I even tried converting the function into a shortcode and then instead of calling the function directly, I did a do_shortcode in single.php, but it incremented the number twice.
I’ve done site wide searches to see if the function is called anywhere else and it is now only in this one template. The function returns the counter to the page and it only outputs once, but that UPDATE statement seems to fire twice.
I’ll keep playing around with it.
Haven’t tried this solution yet but wanted to let you know I received the info and after a quick look I think this will be the key. It looks right to me. Will give it a try later today to confirm. Thank you for your fast and clear response. Much appreciated.
I seem to be having the same kind of issue. I have a function in my functions.php for my theme.
The function looks like this:
function get_views_counter() {
global $post;
$post_id_for_counter = $post->ID;
$current_view_counter = (int) get_field('view_counter', $post_id_for_counter);
if($current_view_counter >= 0) {
$updated_counter = $current_view_counter + 1;
update_field('view_counter', $updated_counter, $post_id_for_counter);
}
$new_view_counter = (int) get_field('view_counter', $post_id_for_counter);
$number_formated_int = (int) number_format($new_view_counter);
$output = $new_view_counter . " views";
return $output;
}
The way I am testing it is I have the database open and i do a query so i can see the value of the view_counter field right in the database. I refresh that query while I am loading the page.
I go to the browser, open an article from the site where this function is fired, and if i echo something out from this function I only see it output once.
Initially when the page is loaded, I see in the database the number will increment once. Then after a few seconds, it increments again. If I sit there and hit refresh I will see it increment by 2 every time.
I had read of some issues with single.php loading twice. I am in single.php so trying to figure what it causing my function to fire twice.
Could it be that it is in the functions file?
I have done something similar in the past. On one client site I have an options page with several repeaters. The client can add new rows, and they can reorder the existing rows but they cannot delete or edit existing rows.
Actually, it’s a little more complicated because the rows created are used to dynamically populate a select field that is used on a specific CPT. They are not allowed to delete a value after that value has been used on a post. So when the options page is loaded a check is done of each value to see if it has been used.
This locking of rows is all done using JavaScript. A JS variable is added that contains a list of the values and if they are used or not and then when then page loads a script runs that locks all the rows that cannot be deleted.
So, yes this can be done, but it requires JS. Using JS you can also remove the ability to change the order of rows as well.
This is not a perfect solution because anyone that knows there way around well enough could get past it if they really wanted to.
This could also be done by creating a field for inputting new data and then creating an acf/save_post action that takes the content in these fields and adds them to the repeater so that the repeater is not actually where the content is added thus removing the need to show the repeater to users in the first place and this would remove the ability to bypass the JS solution.
hi, I ve got same question.
How do you solve it ?
I am not exactly clear on the relationships but I gather that it’s something like
Post Type 1 <> Post Type 2 <> Post Type 3
and you want to show either Post Type 1 on Post Type 3 or vice versa.
To eliminate a lot of extra querying, the first thing I would do is to make these relationships bidirectional. This can be done by coding, although this doc is for bidirectional relationships on the same post type https://www.advancedcustomfields.com/resources/bidirectional-relationships/, or you can use a plugin like https://wordpress.org/plugins/post-2-post-for-acf/ and your can find some additional resources in my github repo https://github.com/Hube2/acf-filters-and-functions.
Then instead of doing queries you just get what you need without doing all the reverse relationship queries.
// get relationship field on current post
$related_x = get_field('1st relatioship field name');
foreach ($related_x as $related_x_post) {
$related_y = get_field('2nd relationship field', $related_x_post->ID);
}
I’m not sure if I fully grasp what you’re trying to accomplish, so I could be way off here… but, if it’s a matter of drilling down deeper and deeper, I could envision using a custom hierarchical taxonomy to do something like this. You select the parent term and it displays the possible child options, over and over.
Something like this:
Car Trouble
– Engine
— Possible Engine Issue 1
— Possible Engine Issue 2
– Gear Box
…and so on.
Again, maybe that’s a stupid idea for your use case. Regardless, by using ACF fields on the actual terms themselves, you could specify a “question” field that is associated with each term and displayed to the user.
So for example, if the user chose “the engine”, on the next screen they would see the question associated with that term. Like “Which of the following symptoms is your engine experiencing?”, then the child terms of “Engine” are displayed.
In any case, good luck with your project!
@psgmarketing Did you get your question answered?
I am looking to do something similar.
Hi! I’m trying to use with my site and have no luck added the code to the functions.php, have I missed something?? My code looks:
function hj_create_vCard( $post_id ) {
/*
* In production code, $slug should be set only once in the plugin,
* preferably as a class property, rather than in each function that needs it.
*/
$post_type = get_post_type($post_id);
// only update the agremiados custom post type on save
if ( "Agremiados" != $post_type ) return;
$vpost = get_post($post->ID);
$filename = $vpost->post_name.".vcf";
//header('Content-type: text/x-vcard; charset=utf-8');
//header("Content-Disposition: attachment; filename=".$filename);
$data=null;
$data.="BEGIN:VCARD\n";
$data.="VERSION:3.0\n";
$data.="FN:".$vpost->post_title."\n"; // get post title
$data.="ORG:" .get_field('nombre_de_la_empresa',$vpost->ID)."\n"; // get acf field value
$data.="EMAIL;TYPE=work:" .get_field('correo_electronico',$vpost->ID)."\n"; // get acf field value
$data.="TEL;WORK;VOICE:" .get_field('telefono_celular',$vpost->ID)."\n"; // get acf field value
$data.="ADR;WORK;PREF:" .get_field('direccion_de_la_empresa',$vpost->ID)."\n"; // get acf field value; // get acf field value
$data.="END:VCARD";
$filePath = "/vcard/".$filename; // you can specify path here where you want to store file.
$file = fopen($filePath,"w");
fwrite($file,$data);
fclose($file);
}
add_action( 'save_post', 'hj_create_vCard' );
Thank you!
For others here is what I did.
add_filter('acf/fields/user/result', 'my_acf_fields_user_text', 10, 4);
function my_acf_fields_user_text($text, $user, $field, $post_id){
$args = array(
'post_type' => 'MY_POST_TYPE',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'user_name',
'value' =>$user->ID,
'compare' => '=',
),
array(
'key' => 'META_TO_COMPARE',
'value' =>0,
'compare' => '=',
),
),
);
$posts = new WP_Query( $args );
$nb_posts = $posts->found_posts;
$text .= ' - ' . $user->user_email . ' [' . $nb_posts . ' posts]';
return $text;
}
This is a little old, but someone I know asked the same question. It can be accomplished with a little work.
Bakery wants a text field, but ACF stores an Attachemnt ID. So what you need to do is to convert the image field into a text field that contains the entire image tag.
Let’s say for example that the field created is named “post_grid_featued_image”.
Create am acf/save_post action
add_action('acf/save_post', 'image_to_image_tag');
function image_to_image_tag($post_id) {
// get unformatted image field value, ensures that image ID is returned
$image_id = intval(get_field('post_grid_featured_image', $post_id, false));
// a variable to build the image tag
$image_tag = '';
// a different custom field name to store our tag
$meta_key = 'post_grid_featued_image_converted';
if ($image_id) {
// if image id is not empty create the image tag
// first get the image see function at WP for more information
// change image size to desired image size
$size = 'medium';
$image = wp_get_attachment_image_src($image_id, $size);
if (!empty($image)) {
// only do this if the image exists
// get image alt text if it exists
$image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
$image_tag = '<img src="'.$image[0].'" width="'.$image[1].
'" height="'.$image[2].'" alt=" '.$image_alt.
' " title="'.$image_alt. '" class="size-'.$size.
' wp-image-'.$image_id.'" />';
// bonus - make image responsive, this is why the class was added above
if (function_exists('wp_filter_content_tags')) {
$image_tag = wp_filter_content_tags($image_tag);
} else {
$image_tag = wp_make_content_images_responsive($image_tag);
}
}
}
// at this point $image_tag will contain the image tag
// or it will be empty because there is no image
// save the value in a new meta_key
$meta_key = 'post_grid_featured_image_converted';
update_post_meta($post_id, $meta_key, $image_tag);
}
Now instead of using the acf field you use the custom field that was created for the grid image that contains the image tag “post_grid_featured_image_converted”
Just wanted to follow-up with an update.
I raised a support ticket about the long-term viability of FC and got a reply from Elliot – basically, yeah this stuff is staying around and being actively developed/improved, and part of the ACF model is to fix an imperfect CMS basically… Really great to hear!
Also opted to go with FC over ACF blocks for the project that brought all these questions up – there was some debate, but we’re OK until WP nukes the Classic “block”.
I have no idea. This line should cause acf to run and save values to the correct post ID because it triggers the sequence of events that are needed.
$post_id = wp_insert_post( $new_post );
The fact that was not happening means that you have larger issues that may or may not be associated with that plugin.
My apologies for the extremely delayed response.
What worked for me was ACF Code Field. In that, we can’t directly add iframes. However, adding iframes within a shortcode works. So, I’m coupling ACF Code field with Advanced iFrame Pro and it’s been working like a charm.
Here’s a working link – https://businessquant.com/intel-revenue-by-segment
Hope it helps
So i have done some additional digging and found out that the validation message appears on Safari if you use any field that is not a field type of Text or Text Area. As soon as you add a field type Text or Text Area it does not validate properly with the ACF error messages, just plain standard Safari html validation “Fill out this field”. I can see that no kind of javascript is fired in the dom to add specific classes such as is-invalid in the form or find any acf notices.
This is how the form markup looks like when validation error occurs on Chrome, firefox, IE11, Edge:
<form id="acf-form" class="acf-form is-invalid" action method="post">
This is how the form markup looks like when validation error occurs on Safari:
<form id="acf-form" class="acf-form" action method="post">
I have tried this on a clean WP installation with only ACF to really exclude any other conflict. So the issue seem to be with validation of field types Text and Text Area where the javascript does not fire off.
Anyone else keen to reproduce this and check? My conclusion is that this is a bug in ACF.
By the way the forum system is very buggy it seems. This is my 10th time trying to submit a reply.
This would remove the entire header on a specific page
<?php
$pageid = null;
if( is_home() && !is_front_page() ) {
$pageid = get_option('page_for_posts');
} else {
$pageid = $post->ID;
}
// replace number with id where you don't want it shown
if ($pagid != 1234) {
?>
<div class="header-default-top-section">
... the rest of your header code here
<?php
}
?>
Yes, I can, I think.
What worked for me is to get the name of the custom field from ACF. Then I placed the name of this field inside square brackets in the region I wanted it to display — like a shortcode. And just like that, the full HTML content from the WYSIWYG field showed up.
Sorry John I found the following code in my template for this group, do you know how I could edit it so that this group doesn’t display on my /online-store page too ?
Many thanks!
<?php
$pageid = null;
if( is_home() && !is_front_page() ) {
$pageid = get_option('page_for_posts');
} else {
$pageid = $post->ID;
}
?>
<div class="header-default-top-section">
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="content-box">
<?php if( get_field('main_title', $pageid ) ) : ?>
<h1><?php the_field('main_title', $pageid ); ?></h1>
<?php endif; ?>
<div class="parting"></div>
<?php if( get_field('text_beside_title', $pageid ) ) : ?>
<p><?php the_field('text_beside_title', $pageid ); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<div class="col">
<?php
$footer = get_field('footer', 47);
if( $footer ): ?>
<h3><?php echo $footer['col-footer']['col-footer-h3'];?></h3>
<?php endif; ?>
<?php if( have_rows('col-footer-ul-li', 47) ): ?>
<ul class="social">
<?php while( have_rows('col-footer-ul-li', 47) ): the_row(); ?>
<li><a href="<?php the_sub_field('col-footer-href'); ?>"><p><?php the_sub_field('col-footer-ul-li-p'); ?></p></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
They may have different keys but there is only one field in the database. The field name is used for the meta_key and these need to be unique for fields saved for the same post.
Sorry, but I do not know of any solution.
Unfortunately, you would need to contact SearchWP or hope that someone sees your question here who has access to SearchWP.
ACF does a standard search query when you use the search field. At a guess I would say that whatever SearchWP is doing to search queries in interfering with the ACFs queries. But without access to the code of the plugin it would be impossible for me to figure out what that is.
You would need to search the for the comic post ID in the post object field of the repeater of the brand post.
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
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.