hi thank you for your answer 🙂
Actually I’m making a generic personnal template with customization from the backend; something like in the premium themes.
So I made a “theme apearance options page”. I added fields like “h1 title” as repeater and “colors, fonts…” as subfields.
I made a specific css for my “customization theme” but to display the ACF fields I’m using a php file enqueued as stylesheet.
When I’m enter the value like “$color =’#fff'” and echo it on the css tag it’s working.
So I think my problem is ACF doesn’t knows which fields i’m calling from this file and I need create a function with an add_action. But may be i’m wrong …
But I don’t know how resolve it
Thx John… copy that – you are absolutely right. But why I’m wondering was the fact that the new Post will show the image from another author but not the gallery items.
we are using the following action to restrict users fro their own media/posts:
if( is_admin() && !current_user_can('edit_others_posts') ) {
$wp_query->set( 'author', $current_user->ID )...
With this function we normale did’t see the ACF groups in a post too.
so we were using this snippet
if (isset($wp_query->query_vars['post_type']) &&
($wp_query->query_vars['post_type'] == 'acf-field' ||
$wp_query->query_vars['post_type'] == 'acf-field-group'
)) { return; }
and to fix the problem with the Gallery we were adding on more condition:
if (isset($wp_query->query_vars['post_type']) &&
($wp_query->query_vars['post_type'] == 'acf-field' ||
$wp_query->query_vars['post_type'] == 'acf-field-group' ||
($pagenow == 'post.php')
)) {return; }
cheers
Based on your resource page for displaying a random row, the code for doing this has changed to the following. I tried the code above and it didn’t work, but the code stated on the resource page does work to display a random row perfectly. My question now is how to mod it to display a random set of rows, not just one. Note, I tried adding that comma and a number that’s in your example, but it broke the whole output and displays nothing.
<?php
$rows = get_field('repeater_field_name' ); // get all the rows
$rand_row = $rows[ array_rand( $rows ) ]; // get a random row
$rand_row_image = $rand_row['sub_field_name' ]; // get the sub field value
// Note
// $first_row_image = 123 (image ID)
$image = wp_get_attachment_image_src( $rand_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
The problem is that nothing in WP is loaded if you are trying to load the content of a specific PHP file using AJAX.
In order to have access to all WP and plugin function you must make all request through /wp-admin/admin-ajax.php, setting the required “action” and creating the needed WP filters rather than requesting a specific file. This page explains AJAX in WP, it says AJAX in Plugins, but the information is relevant to all AJAX requests in WP.
The only way to do this would be to add custom JavaScript under the correct conditions to set the fields to readonly, or otherwise disable them. It is possible using the acf/load_field filter to set fields to read only or disabled, but this does not work with all ACF fields do to the jQuery libraries used for those fields.
What I meant was to just display the data without formatting it to look like an input field.
WP does not include any way to order posts by category. If you were doing
– Category
– – Series
– – – Date
I would tell you to get the posts in each term separately, but since the category is the last thing, that won’t help you.
The only way that I can see to do this is to loop through the posts twice. The first time creating a separate array to store them in and somehow ordering them the way you want and then the second time to show the posts you stored in the array. But I’m not really sure how to do that.
Another choice would be to somehow do multiple queries, not sure about this either. Maybe do a query for each for each season in each series ordered by date.
Sorry I can’t be of more help.
Hi John, nop, the “key” field is “key__classes__content_row__container”, i named with “key_” my key fields and without “key” the names, so for any field i allways have:
Field keys must start with “field_”, if your using “key_” then that’s probably part of your problem.
It’s easy to miss in the field settings section https://www.advancedcustomfields.com/resources/register-fields-via-php/
$field = array (
/* (string) Unique identifier for the field. Must begin with 'field_' */
'key' => 'field_1',
Hi John, nop, the “key” field is “key__classes__content_row__container”, i named with “key_” my key fields and without “key” the names, so for any field i allways have:
key: key__classes__content_row__container
name: classes__content_row__container
If i apply the filter using the key “key__classes__content_row__container” it apply for any field everywhere, and that´s the thing i don´t want.
I need to apply a filter to that cloned field but just in that place, not all the cloned fields using that field.
This is the final field, that´s a cloned one inside a flexible field:
<input type="text" id="acf-key__flexible_content_rows-5b8d6b7c7ff95-key__layout_html_row__content_key__r_builder_classes_group-key__classes__content_row__container" name="acf[key__flexible_content_rows][5b8d6b7c7ff95][key__layout_html_row__content_key__r_builder_classes_group][key__classes__content_row__container]" value="container" placeholder="container">
And this is the same field but in another flexible layout:
<input type="text" id="acf-key__flexible_content_rows-5b8d6cb87ff96-key__layout_template_row__content_key__r_builder_classes_group-key__classes__content_row__container" name="acf[key__flexible_content_rows][5b8d6cb87ff96][key__layout_template_row__content_key__r_builder_classes_group][key__classes__content_row__container]" value="container" placeholder="container">
The first one is inside “key__layout_html_row__content” (flexible layout)
Second in “key__layout_template_row__content” (flexible layout)
If i apply the filter by key using “key__classes__content_row__container”, it applys for all places but i want to apply only for a particual place, let´s say the first one.
I know it´s very complex my situation….
Thanks John for the reply.
What I wish to do is create a page to display the data of a custom post type on the frontend, so in that page all the fields should be “read only” and not editable.
I can create a page template for this purpose and display the data using HTML and PHP code. However my custom post type has multiple custom fields from different types – text, date, photos, reference to other posts, etc. Therefore it will require some coding to display and style all the information correctly (i.e. using rows, grouping fields together, etc.).
Since acf_form already renders all the fields in the right structure on the frontend, I thought there might be some way to use it (or something else) also for the purpose of displaying the data without editing it.
See the Dynamic $_GET parameters section on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
You best change to get information on GF is to ask support for that plugin.
Some of the questions you need to answer are:
Glad you got this worked out, I was about to tell you to look for a filter that was altering the post slug as soon as I read this.
– When creating new fields (any field, not just a repeater) the key will change from the field_xxxxxxxxxxxxxx format to be the same as the name i.e short-description.
and then I scrolled down to see that you’d solved it.
My main reason for posting is to say thank you for the way you posted your problem. If you hadn’t solved this problem yourself the amount of detail you gave made it extremely easy to narrow down what the underlying issue was.
function custom_scripts() {
$theme = get_field('global_theme', 'option');
$stylesheet = ($theme == 'alt' ? '/styles/style.css' : '/styles/style-alt.css');
wp_enqueue_style( 'custom-style', get_template_directory_uri() . $theme );
}
add_action( 'wp_enqueue_scripts', 'custom_scripts' );
What part of your code is not working? The query or the display? Is it getting the posts?
Going to be honest, by looking at the code you posted I don’t really understand what you are doing or why. I think that in order to grasp it fully I’d need to see it in action and be able to look at the field group(s)?
Can you attach the JSON export for the field group(s) in question? Export them to JSON and then put them in a .zip file and you should be able to upload them here. Or if you want you could create a GIST or put the code online in some way so that I can see the field group(s)
I have the same question! I would really like to prepare with this new function. 🙂
Edit: so I installed the Early access version of ACF (non-pro) and it doesn’t have the acf_register_block
function either. So it hasn’t dropped yet in both versions. I really though it was already there when I was reading the article 🙁
Any word from the developers when this is going to land?
Thank you so much for responding, John.
I believe we’re talking about slightly different things.
Let me share some code with you.
$fields_goals = array(
'ryit_user_legacy_vision', //vision
'ryit_user_legacy_mission', //mission
'ryit_user_goals_ten_year', //ten year goal
'ryit_user_goals_five_year', //five year goal
'ryit_user_goals_one_year' //one-year goal
);
$fields_interests = array(
'ryit_user_interests_expertise', //fields of interest or skill
'ryit_user_interests_study' //more info about your interests
);
$fields = array(array("Vision & Roadmap", $fields_goals), array("Purpose & Business", $fields_interests));
$active_field = 'vision-roadmap';
$echo .= '<div class="main" active-field="' . $active_field . '">';
//Set up category tabs
$echo .= '<ul class="tabs">';
foreach($fields as $field) {
$echo .= '<li id="tab-' . sanitize_title($field[0]) . '"';
if($active_field == sanitize_title($field[0])) {
$echo .= 'class="active"';
}
$echo .= '>' . $field[0] . '</li>';
}
$echo .= '<li id="tab-all">View all</li>';
$echo .= '</ul>';
$fields_with_val = 0;
$fields_echo = "";
//Set up fields inside categories
foreach($fields as $field_group) {
$echo .= '<div class="field-group" id="field-group-' . sanitize_title($field_group[0]) . '">';
$fields = $field_group[1];
$fields_echo = "";
foreach ($fields as $field) {
//get essential profile data
$field_obj = get_field_object($field, 'user_' . $user_id);
if(!empty($field_obj['value']) || ($user_id == $current_user_id)) {
$fields_with_val++;
$can_edit = ($user_id == $current_user_id) ? " can-edit" : "";
$fields_echo .= '<div id="' . $field_obj['name'] . '" class="field' . $can_edit . '">';
$fields_echo .= '<h3>' . $field_obj['label'] . '</h3>';
$fields_echo .= '<div class="field-data">';
if(empty($field_obj['value']) && ($user_id == $current_user_id)) {
$field_echo = '<div class="field-content"><p class="instructions">Instructions: ' . $field_obj['instructions'] . '</p><p class="add-response button">Add your response</p></div>';
}
else {
if(is_array($field_obj['value'])) {
$i = 0;
$val_output = "";
foreach($field_obj['value'] as $val) {
if($i>0) {
$val_output .= ", ";
}
$val_output .= $val;
$i++;
}
$field_echo = '<div class="field-content">' . $val_output . '</div>';
}
else {
$field_echo = '<div class="field-content">' . $field_obj['value'] . '</div>';
}
}
$fields_echo .= $field_echo;
//echo "user id: " . $user_id;
//echo "current user id: " . $current_user_id;
if($user_id == $current_user_id) {
$settings = array(
'post_id' => 'user_' . $user_id,
'html_updated_message' => '',
'fields' => array($field),
'id' => 'form_' . $field_obj['name']
);
ob_start();
acf_form($settings);
$form = ob_get_clean();
$fields_echo .= $form;
}
$fields_echo .= '</div>'; //end field-data
$fields_echo .= '</div>'; //end field
}
}
Now, look at where I define the $fields_goals array.
The first two array entries refer to fields ‘vision’ and ‘mission’ inside the group ‘ryit_user_legacy’ and the last three fields belong inside ‘ryit_user_goals’.
When I define them with their alphanumeric names like this, the front end shows the values currently visible in the backend. But when I SAVE them, the values don’t change. Nothing gets saved.
When I change to using the field KEYS however, I manage to save the values, but the values don’t show on the profile in the backend. In other words, the KEY sends the data SOMEWHERE, but it doesn’t show up on the profile.
This seems like a bug to me, but I’m not sure.
Everything works fine with fields which aren’t inside of groups. I’m stumped.
Eivind
Hi Hidieh:
You can use the id parameter (one of the arguments passed to acf_form) to create a unique id for each of your forms. In your function triggered by the submit form action then use the global for acf_form to retrieve the ID value ($GLOBALS[‘acf_form’]).
See Elliot’s responses in this thread (though the original question is different from what you are asking) for more detail:
https://support.advancedcustomfields.com/forums/topic/acf_form-id-not-being-set-from-args/
Hope this is helpful!
5.7.4 available but returning “bad request”
@colinsafranek thanks for providing your code.
I’ve optimized it a little bit, to be more flexible with the amount of columns:
/* ACF "repeat-horizontal" class, display repeaters in horizontal columns */
.repeat-horizontal .acf-repeater tbody {
display: flex;
flex-direction: row;
}
.repeat-horizontal .acf-repeater tr.acf-row:not(.acf-clone) {
width: 100%;
}
.repeat-horizontal .acf-repeater tr.acf-row:not(.acf-clone) td.acf-fields {
width: 100% !important; /* important is necessary because it gets overwritten on drag&drop */
}
Sooooo…y’all were super helpful before and I was able to apply this filtering to another website as well. All was going swimmingly until the client asked for one more level of sorting, and I’m tearing my hair out trying to figure this out.
So, this is for a choral group. They’re wanting to display all their past seasons, sorted by year/season, and then by performance date (performances are actually individual products in WooCommerce). Sorting by the ACF meta fields is going fine. But within each year, they also want to break it out to sorting all the concerts assigned the ‘cantata-series’ product category, and the ‘masterworks-series’ product category. So, a typical listing would look something like:
2017-2018 SEASON
Cantata Series
* Concert Date 1
* Concert Date 2
Masterworks Series
* Concert Date 1
* Concert Date 2
2016-2017 SEASON
Cantata Series
* Concert Date 1
* Concert Date 2
Masterworks Series
* Concert Date 1
* Concert Date 2
etc. etc. etc.
I was doing fine when it was just sorting by season and then by date. But adding in sorting by category is just throwing me. I can get it to display the correct series before each and every date it applies to, but that’s not right.
Here’s my code right now (not working)
$season_args = array(
'numberposts' => -1,
'post_type' => 'product',
'product_cat' => 'past, cantata-series, masterworks-series',
'meta_query' => array(
'season_clause' => array(
'key' => 'select_season',
'compare' => 'EXISTS',
),
'date_clause' => array(
'key' => 'date',
'compare' => 'EXISTS',
)
),
'orderby' => array(
'season_clause' => 'ASC',
'date_clause' => 'ASC'
)
);
$the_query = new WP_Query($season_args);
if ($the_query->have_posts()) {
// variables to hold values from previous post
$last_season = '';
$last_date = '';
while ($the_query->have_posts()) {
$the_query->the_post();
// get season and compare it to the previous post
$this_season = get_field('select_season');
if ($this_season != $last_season) {
// the season has changed
if ($last_season != '') {
// close the post UL, season DIV, and season DIV that we'll open next
// this will be skipped if this is the first post
// because $last_season will be empty
echo '</div><!-- .season -->';
} ?>
<div class="season">
<h2><?php echo $this_season->post_title; ?></h2>
<?php // clear $last_season
$last_season = '';
// set $last_season to $this_season
$last_season = $this_season; ?>
<?php if ( has_term( 'cantata-series', 'product_cat' ) ) { ?>
<h3>Cantata Series</h3>
<?php }
} // end if new season
// get the date and compare to previous post
$this_date = get_field('date', false, false);
$this_date = new DateTime($this_date); ?>
<?php if ( has_term( 'cantata-series', 'product_cat' ) ) { ?>
<li><?php echo $this_date->format('M j Y'); ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php } ?>
<?php } // end while have posts
} // end if have_posts
wp_reset_query();
Thanks in advance.
~Laura
You need to use WP_Query, see the usage information at the top [wp_reset_postdata()]
You should avoid query_posts() because it overwrites the main query. This function is rarely safe to use https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts
There isn’t any easy way to change the field name of a field once it has been used. The use of “_thumbnail_id” for the field name is something that you really need to do from the start before any data is added to a site.
In order to update every post you would need to do a query to get every post, loop through them, get the value from the ACF field and then update the post thumbnail field.
Yes, that was it.
didn’t find nothing on https://www.advancedcustomfields.com/resources/checkbox/ and thought it was a regex that anything after the first “:” would be the label, never thought it would require a space 🙂
thanks!
So, I posted a response with the solution but the site seems to have deleted it. Here it is again:
John, thanks for the response. You were correct in your assessment. Unfortunately WP Google Maps was not possible for this project, although I do use that plugin frequently (it’s great).
My Coworker Steve was able to solve this particular issue for me. I’ll post that solution below:
So the goal here was to display an archive of the closest 3 locations on the single page of the custom post type in question. Each post is using the ACF Google Map field and has associated Google Map data.
in functions.php (or mu-plugin):
function get_distance($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$dist = $dist * 60 * 1.1515;
return $dist;
}
In the custom post type single page:
<?php
$current_post = get_the_ID();
$location = get_field('map_pin'); // Adjust to your Google Map field name
$current_post_lat = $location['lat'];
$current_post_long = $location['lng'];
$query = new WP_Query( array(
'post_type' => 'post-type-name',
'posts_per_page' => -1,
'post__not_in' => array($current_post)
)
);
$post_1_dist = 100;
$post_1 = "0";
$post_2_dist = 101;
$post_2 = "0";
$post_3_dist = 102;
$post_3 = "0";
while ($query->have_posts()) : $query->the_post();
$location = get_field('map_pin');
$dist = get_distance($current_post_lat, $current_post_long, $location['lat'], $location['lng']);
if ($dist < $post_1_dist){
$post_3 = $post_2;
$post_3_dist = $post_2_dist;
$post_2 = $post_1;
$post_2_dist = $post_1_dist;
$post_1 = get_the_ID();
$post_1_dist = $dist;
}
if (($dist < $post_2_dist) && ($dist > $post_1_dist)){
$post_3 = $post_2;
$post_3_dist = $post_2_dist;
$post_2 = get_the_ID();
$post_2_dist = $dist;
}
if (($dist < $post_3_dist) && ($dist > $post_1_dist) && ($dist > $post_2_dist)){
$post_3 = get_the_ID();
$post_3_dist = $dist;
}
endwhile; wp_reset_postdata(); ?>
//Results
<?php echo get_the_title($post_1); ?>
<?php echo get_the_title($post_2); ?>
<?php echo get_the_title($post_3); ?>
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.