Something like this should do what you need:
<?php
$images = get_field('section_gallery');
if( $images ): ?>
<?php foreach( $images as $image ): ?>
<div>
<a data-fancybox="gallery" href="<?php echo $image['sizes']['large']; ?>">
<img src="<?php echo $image['sizes']['medium']; ?>" alt="<?php echo $image['alt']; ?>">
<?php if ($image['caption']) { ?><span><?php echo $image['caption']; ?></span><?php } ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
get_field('FILED NAME', "term_{$TERM_ID}");
To do that you would need to have a database full of locations or do the math to do all the calculations based on the longitude and latitude. Without some type of database you’d need to get all the posts/fields and do the calculations 1 at a time? Sorry I can’t give you more help than that https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula.
I use the pro/gold version of WP Google Maps, it does all of this for me https://www.wpgmaps.com/purchase-professional-version/
You must edit the theme code or you must use action/filter hooks provided by your theme to add code and or content to the site.
I don’t know anything about how your theme works and it is unlikely that you’ll get answers here unless someone that has experience with this theme sees your question.
The best source of information on how to make changes to your theme to show fields created using ACF would be the developer you purchased your theme from.
Once you know where and how to make your changes then someone here might be able to help you with ACF related code.
Sorry I didn’t get back to you sooner, I’ve had a crazy week. Glad you got it worked out.
Your last question was correct, it would not be updated if the related post is updated. I would probably go with some alternate approach still other than using those joins because of site speed…
…though I don’t know how I would do that. When the post is updated at the other end I would probably do something like doing a query to get all the posts that have the relationship and update all of the values for those posts. This would slow down the admin when saving posts but that to me is preferable to slowing down the front end of the site.
Hmz, sorry for the spam, but after some rethinking, my earlier reply did not in fact solve my problem. I do need to use the custom post type in my flexible content field…
So back to my initial question; how do I get custom post type data like title, content and excerpt through a flexible content field?
This is the final solution I’ve come up with. I found the function which returns all the local fields, compared them to the field groups I expected in my plugin, then looped through the names, breaking out of the loop on the first match.
I doubt this will work in most setups, duplicate names are a possibility, you could probably check against the 3rd parameters $post_id
for a post type, then the post type against $local->groups and each groups location parameters. #worksForMe
/**
* Get local field by name
*
* @param String $reference - Field unique ACF key
* @param String $field_name - Field nice name/slug
*
* @return String $reference
*/
function get_field_by_name( $reference, $field_name ) {
$local_fields = acf_local(); // Grab all local fields
// Ensure we have local fields to work with
// Ensure that reference is empty
if( empty( $local_fields ) || ! empty( $reference ) ) {
return $reference;
}
// Loop through groups
foreach( $local_fields->fields as $key => $field_arr ) {
/**
* Ensure the field is in our expected groups.
* Removed when not in the context of plugin
* $this->field_group_keys = array( 'group_xyz' ) - Holds array of expected local field groups
*/
// if( ! in_array( $field_arr['parent'], $this->field_group_keys ) ) {
// continue;
// }
// Break the loop on first found match
if( $field_name === $field_arr['name'] ) {
$reference = $key;
break;
}
}
return $reference;
}
add_filter( 'acf/get_field_reference', 'get_field_by_name', 100, 2 );
Thanks, that helped me track it down. I think my problem was two-fold: I was using acf/update_field/name={field_name}
, where it should have been acf/update_value/name={field_name}
, and also, as you described, the key was required instead of the name for this repeater field. Thanks much!
Hello.
Maybe I was not too clear, not my native language, sorry. For some of my requests I was almost sure were possible, so I was asking how to do them easily as I can’t manage too much code, but if you point me to those, maybe I can do it.
1) I meant if all the ACF are now activated by default as I see in previous versions there were add-ons to activate when needed;
2-3) i will MANUALLY map form fields–> custom post type fields but I can do it if a settings page or so exists in ACF plugin where I can do it. A connecting feature where I can assign the CPT and its fields have to be transferred coming from front end submitted data through a form. Is there an option, a feature like this? I saw it in another plugin.
4) I will try with an external importer plugin. If ACF allows it, I’m ok.
CF 7: I will be very happy if I don’t need it, but is there a section in ACF to do it’ is there a tutorial where I can see how it works?
Thank you so much for your patience but I’m planning to buy several licenses when I will be able to build correctly this “complete system”, so I can use it for books, jobs, films, real estates … directory sites.
1) You will need to do a lot of coding of filters make this work.
2) There isn’t any way to automatically map fields from other plugins to ACF
3) See the above
4) ACF does not allow importing. You need to look at an importing plugin like http://www.wpallimport.com/, the pro version includes an ACF add on.
Outside of your questions, there isn’t a reason to use CF7 for your forms. With ACF you can build front end forms and using the acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/ you can set new posts as drafts that need to be approved.
Using another form plugin, the filters involved would be too complicated to go into here and would need to be filters that work for the form plugin you’re using and also save the data in a way that ACF will understand using calls to ACF functions. Is what you want to do possible? Yes, but any way you look at it there will be complex coding involved.
When the field is at the top level if could be a priority issue. Try setting the priority of your filter to 20.
As for when it is in a repeater field, the reason for this is that the field name is not what you would expect. The field name in this case is constructed using this formula
"{$repeater_name}_{$row_index}_{$sib_field_name}"
More than likely the filter is not being called at all. For the case of this filter and repeater sub fields you need to use the field key rather than the field name. You will need to use the 3rd parameter that is passed to your filter to get other information when needed about the field.
This thread helped me figure out the answer. Make sure look at extrasmall’s answer to get it working.
The posts_join
worked. Posting code below in case it helps anyone else.
$args = array(
'post_type' => 'artworks',
'posts_per_page' => -1,
'no_found_rows' => true,
'order' => 'ASC',
'orderby' => 'artist_name_artwork_title',
);
add_filter('posts_join', 'test_join', 10, 2 );
function test_join($joins, $wp_query) {
if ( $wp_query->get( 'orderby' ) != 'artist_name_artwork_title' ) {
return $joins;
}
global $wpdb;
$joins .= " LEFT JOIN $wpdb->postmeta name ON name.post_id=$wpdb->posts.ID AND name.meta_key='artists_name'" ;
$joins .= " LEFT JOIN $wpdb->postmeta lastname ON lastname.post_id=name.meta_value AND lastname.meta_key='artist_last_name'";
$joins .= " LEFT JOIN $wpdb->postmeta firstname ON firstname.post_id=name.meta_value AND firstname.meta_key='artist_first_name'";
return $joins;
}
add_filter('posts_orderby', 'orderby_artist_name_artwork_title', 10, 2);
function orderby_artist_name_artwork_title($orderby_statement, $wp_query) {
if ( $wp_query->get( 'orderby' ) != 'artist_name_artwork_title' ) {
return $orderby_statement;
}
global $wpdb;
$orderby_statement = "lastname.meta_value, firstname.meta_value, $wpdb->posts.post_title";
return $orderby_statement;
}
$wp_query = new WP_Query( $args );
remove_filter('posts_join', 'test_join', 10 );
remove_filter('posts_orderby', 'orderby_artist_name_artwork_title', 10 );
I know you marked this solved, but I just purchased Pro and was immediately upgraded to 5.7.3 but I’m having what sounds like the same issue that you were having before. I created a few Fields, and I believe correctly, but I’m also attempting to use Tabs to display content in a more compact way on our site. I’m using Elementor and when I try to link the dynamic content no matter what I choose in the key settings, only one of the fields that I created are populated. The whole list is not there. There is a list if I choose Post Custom Field instead of ACF Field but most what populates is either the word Array or some 4 digit number. There are only a couple of tabs that I can actually get to work and they are text based. So it appears that things are not populating into the key as a selection from what I input.? Here is a screen shot. Thank you in Advance and if I am posting in the incorrect place please let me know and I’ll go where I’m supposed to. I’m just not sure where to go. Since I just started I was dealing withe a huge learning curve but catch on pretty quickly so I think I do have an understanding how it is supposed to work.
I actually solved this! I didn’t do it the way I thought it would be, but I got it to work the way I needed it to work.
function workshop_dates( $where ) {
$where = str_replace("meta_key = 'workshop_details_%", "meta_key LIKE 'workshop_details_%", $where);
return $where;
}
add_filter('posts_where', 'workshop_dates');
// vars
$date_now = date('Y-m-d H:i:s');
// args
$args = array(
'post_type' => 'page',
//'posts_per_page' => 2,
'meta_query' => array(
array(
'key' => 'workshop_details',
'compare' => '>',
'value' => 0
)
)
);
I basically pulled all of the fields, and once I had them, I displayed the nested repeater fields.
Thanks for your reply @squarecandy, I’ll take a look.
Hmm, yeah. Much harder if you already have a field name with data that you need to preserve. See this thread: https://support.advancedcustomfields.com/forums/topic/changing-field-name-question/
came here for the same thing, for example:
this works:
while(have_rows($listaEventoskey,’option’)){
the_row();
$thisid = get_sub_field(‘eventid’);
echo “i have this: $thisid<br>”;
}
but, this won’t work:
function infoEvento() {
while(have_rows($listaEventoskey,’option’)){
the_row();
$thisid = get_sub_field(‘eventid’);
echo “tenho o id de $thisid<br>”;
}
}
infoEvento();
Thanks @squarecandy, but if I’ve got an existing image field and I replace the name field from my field group, all my old images go away.
How can I keep them if I change the field name?
Thanks in advance!
It can be difficult, I know that it can sometimes be quite a task to get clients to understand it all. It would be better if the WP auto draft feature included custom fields, but I doubt that will happen. One of the major issues is that if the post title, post content or excerpt for the post have not been edited, the auto save doesn’t even fire to begin with. There is a solution to save that ACF fields during the auto save, but it depends on the auto save actually happening and that’s where it falls apart since the WP JS only looks for updates to built in fields. https://github.com/elliotcondon/acf/issues/585
When I say duplicate fields I don’t necessarily mean that I’d create an ACF field on both field types that match. What I mean is that I would create a custom field using the standard WP functions like add_post_meta() with a different field name. The only purpose for this field name is to have a value to search by in the database. I outlined a similar idea about repeater fields here https://acfextras.com/dont-query-repeaters/
I missed the post join part, and going to be honest, I don’t know how to do that. I try to avoid anything that is likely to slow a site and adding extra joins in db queries can really slow down a WP site.
There isn’t an easy way to update all of the existing posts. Basically, you need to do a query, get all the posts, loop though them, get the related post and update the values. If you have a lot of posts on your site doing this will likely just timeout the browser trying to do it.
Thanks, @hube2. I was afraid of that. I do have the values from “Post Type B” loading into fields in a few posts of “Post Type A” using acf/load_value but would then have to go back and update all posts in “Post Type A” to get those values loading in the database. Thought I would exhaust the posts_join
option before doing that since it seemed similar in concept to the acf/post-object/query filter. (And is there a way to bulk update all the posts of “Post Type A”?)
And here is my code for Bootstrap 4.
I change the IDs name (#accordion) because I want to use multiple accordions on one page.
<?php if( have_rows('_accordion_zeile') ): $accordion++; ?>
<div id="accordion<?php echo $accordion; ?>">
<?php while ( have_rows('_accordion_zeile') ) : the_row(); $collapse++; ?>
<div class="card">
<div class="card-header" id="heading<?php echo $collapse; ?>">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#collapse<?php echo $collapse; ?>" aria-expanded="true" aria-controls="collapse<?php echo $collapse; ?>">
<?php the_sub_field("_accordion_titel"); ?>
</button>
</h5>
</div>
<div id="collapse<?php echo $collapse; ?>" class="collapse" aria-labelledby="heading<?php echo $collapse; ?>" data-parent="#accordion<?php echo $accordion; ?>">
<div class="card-body">
<?php the_sub_field("_accordion_inhalt"); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
Meanwhile, I’ve converted the Relationship field to a Post Object field since each Artwork only has one Artist associated with it. That has this post object query filter:
function artist_post_object_query( $args, $field, $post_id ) {
$args['meta_query'] = array(
'relation' => 'AND',
'last_name' => array(
'key' => 'artist_last_name'
),
'first_name' => array(
'key' => 'artist_first_name'
));
$args['orderby'] = array(
'last_name' => 'ASC',
'first_name' => 'ASC'
);
// return
return $args;
}
add_filter('acf/fields/post_object/query/name=artists_name', 'artist_post_object_query', 10, 3);
So Artist correctly alphabetized in the Artwork edit screen. (Although that filter doesn’t extend to the List screen sort order, for which I’m using Admin Columns Pro.)
Essentially I want that filter to work any time the Artworks field ‘artist_name’ is being used as a meta_key to sort so that ‘meta_value’ can be replaced with ‘artist_last_name’ + ‘artist_first_name’ instead of the post ID. It seems like a posts_join
and then posts_orderby
filter would work but I can’t get this join to work:
add_filter('posts_join', 'artist_join', 10, 2 );
function artist_join($joins)
{
if(is_post_type_archive('artworks') || (is_admin() && $_GET['post_type'] == 'artworks')) {
global $wpdb;
$joins .= " INNER JOIN {$wpdb->postmeta} pm_last_name ON pm_last_name.post_id={$wpdb->posts}.ID
WHERE pm_last_name.meta_key='artist_last_name'" ;
$joins .= " INNER JOIN {$wpdb->postmeta} pm_first_name ON pm_first_name.post_id={$wpdb->posts}.ID
WHERE pm_first_name.meta_key='artist_first_name'" ;
}
return $joins;
}
Any help would be much appreciated.
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.