Hello @keithlock, thanks for your help. But I dont have problem with “True/False” field, I need link the_query to another post_type with relationship field. Logic of query should be like this, but meta_query doesnt support “post_type”:
<?php
$args = array(
'post_type' => 'branch_clinic',
'posts_per_page' => 10,
'meta_query' => array(
array(
'post_type' => 'hospital',
'key' => 'template-part-clinic-heading__highlight',
'value' => '1',
),
),
);
$the_query = new WP_Query( $args );
?>
“template-part-clinic-heading__highlight” is TRUE/FALSE field in HOSPITAL post type, and this hospital is linked by relationship field to BRANCH CLINIC.
Thanks,
Jakub
Hi Keith,
But… it’s better probably to use a Relationship field to make for smarter editing. But we have to keep the data in synch on our own from the way I understand it. So… each time a change is made to the ‘Doctors’ Relationship field when editing ‘Locations’, you can write logic that loops through and updates the ‘Doctors’ records (i.e. adds a location or removes a location).
This is exactly the solution I’m after. I’d like to setup bidirectional relationships between ‘Doctors’ and ‘Locations’ so when I edit a ‘Location’ I can also add/remove ‘Doctors’ from that ‘Location’ and vice versa.
This would be a great Guide on how to create Bidirectional Relationships between two different CPTs.
Any idea on how I can do this?
Hi Seth,
Maybe I am thinking about this incorrectly… but… it seems to me that the caption *should* stay with the image, and *not* the post, yeah?
What if you un-attach the image from the post, then the Caption remains, right?
Wait.. now that I typed that out… I suppose what can be done is…
1) Create an ACF field attached to Images…
2) Make it read only (i.e. disable it using the acf/prepare_field filter). That way.. it *stays* with the image, but cannot be edited by anyone.
Now… when you attach that same image to another Post… the data will carry over and stay consistent.
This is the first time hearing of IPTC for me, but here are docs on how to parse the data with PHP.
To attach the field to Images (so that it can be edited in the Media Library), choose ” ‘Attachment’ ‘is equal to’ ‘All image formats’ ” in the Location Rules when creating/editing the ACF Field Group.
Now, I suspect you can use this filter to do you IPTC parsing work, and saving the data into your Custom field. It’s for “saving posts” but can be used for other data types too. I haven’t worked out precisely how to do this final part yet, but I am confident it’s doable. Let me know what you think.
Hello…
Good question…. here’s how I might tackle it…
First… as we know…
We have a ‘Doctors’ CPT
… with a ‘Locations’ Relationship field.
This ‘Locations’ field may have a series of Location IDs (say: 5, 6, 7)
Now, we have the ‘Locations’ CPT
When looking at ‘Edit Locations’
… we could have a ‘Doctors’ Relationship field here.
In it… we could work this hook…
… to fetch all the Docs that are attached to that Location.
Now, as an alternative… if you just wanted to do this for ‘Display’ purposes, you can pre-populate another field with the doc data… just use some other field type other than Relationship… like the ‘Enhanced Message’ add-on that allows for PHP, or maybe some other built-in field type like textarea, and make it read-only. Example here for making it read-only.
But… it’s better probably to use a Relationship field to make for smarter editing. But we have to keep the data in synch on our own from the way I understand it. So… each time a change is made to the ‘Doctors’ Relationship field when editing ‘Locations’, you can write logic that loops through and updates the ‘Doctors’ records (i.e. adds a location or removes a location).
Now, I haven’t thought far enough ahead to point you how to do the last part, because I’m not sure you wanted to get this far down the rabbit hole 🙂 Let me know!
Hi Keith,
Thanks for getting back to me!
I think this is a little confusing because there is an overlap in terminology. I’m referring to the title attribute you can put on <a>
tags.
<a class="button" href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>" title="TITLE ATTRIBUTE TEXT"><?php echo $link['title']; ?></a>
HOWEVER, I just did some more research about the title attribute for accessibility and SEO and have found that it makes no difference for either of these things. So you can disregard my request completely!
I am now going to reach out to our SEO audit tool and ask them why this is part of their grading/criteria.
Thank you,
get_page has been deprecated… it is recommended to use get_post() in favour of it.
With that said… would a custom WP_Query be a better choice in this scenario?
If you agree… check out an example below of the arguments that you could pass to the WP_Query when using a True/False ACF field.
Hopefully you can pick apart and make sense of it for your use case:
$projects_args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'relation' => 'AND',
'meta_query' => array(
array(
'key' => 'cc_project_complete',
'value' => FALSE,
'compare' => '='
),
array(
'key' => 'cc_project_customer',
'value' => $customer_id,
'compare' => '='
)
),
'fields' => 'id, title, cc_project_job_num, cc_project_load_ship_date, cc_project_asap, cc_project_po_received_date, cc_project_dept_planned_1, cc_project_dept_planned_2, cc_project_dept_planned_3, cc_project_dept_planned_4, cc_project_dept_planned_5, cc_project_dept_planned_6, cc_project_dept_planned_7, cc_project_dept_planned_8, cc_project_dept_planned_9, cc_project_dept_planned_10, cc_project_dept_planned_11, cc_project_dept_planned_12, cc_project_dept_planned_13, cc_project_dept_planned_14, cc_project_dept_planned_15, cc_project_urgency, cc_project_asap_lead_time, cc_project_rush_lead_time, cc_project_standard_lead_time, cc_project_complete, cc_project_customer'
);
‘cc_project_complete’ is my True/False ACF field. Also… you can decide what fields to return using ‘fields’. And in this example… I made sure that 2 criteria were satisfied by using ‘relation’ => ‘AND’ with my ‘meta_query’
Hope that steers you in the right direction.
Hi @kubajjz
Below are the arguments for a WP_Query I created recently that hopefully you can pick apart and make sense of for your use case:
$projects_args = array(
'post_type' => 'projects',
'post_status' => 'publish',
'relation' => 'AND',
'meta_query' => array(
array(
'key' => 'cc_project_complete',
'value' => FALSE,
'compare' => '='
),
array(
'key' => 'cc_project_customer',
'value' => $customer_id,
'compare' => '='
)
),
'fields' => 'id, title, cc_project_job_num, cc_project_load_ship_date, cc_project_asap, cc_project_po_received_date, cc_project_dept_planned_1, cc_project_dept_planned_2, cc_project_dept_planned_3, cc_project_dept_planned_4, cc_project_dept_planned_5, cc_project_dept_planned_6, cc_project_dept_planned_7, cc_project_dept_planned_8, cc_project_dept_planned_9, cc_project_dept_planned_10, cc_project_dept_planned_11, cc_project_dept_planned_12, cc_project_dept_planned_13, cc_project_dept_planned_14, cc_project_dept_planned_15, cc_project_urgency, cc_project_asap_lead_time, cc_project_rush_lead_time, cc_project_standard_lead_time, cc_project_complete, cc_project_customer'
);
‘cc_project_complete’ is my True/False ACF field. Also… you can decide what fields to return using ‘fields’. And in this example… I made sure that 2 criteria were satisfied by using ‘relation’ => ‘AND’ with my ‘meta_query’
Hope that steers you in the right direction.
Not sure what you mean.
When using the Link field with a Return Value of ‘Link Array’ you can get ‘title’ like so:
<?php
$link = get_field('link');
echo $link['title'];
// Button Example
?>
<a class="button" href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a>
It is the ‘Link Text’ in the dialogue box when adding the link. Know what I mean?
Hi @cr101
Here’s a tutorial for how to do that with ACF.
Hi @designlobby
Groups just have one row.. but you can use a Repeater of Groups.
I did this for a test:
Repeater: highlights
—> Group: highlight
——> Text: title
——> Text: desc
——> Link: link
——> Image: image
Code example:
<?php
if ( have_rows('highlights') ) {
while ( have_rows('highlights') ) { // Repeater Loop
the_row();
if ( have_rows('highlight') ) {
while ( have_rows('highlights') ) { // Group Loop
the_row();
$title = get_sub_field('title');
$desc = get_sub_field('desc');
$link = get_sub_field('link');
$image = get_sub_field('image');
echo '<h2>' . $title . '</h2>';
?>
<a href="<?php echo $link['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
<p><?php echo $desc; ?></p>
<?php
}
}
}
}
?>
@John Huebner actually @beee has a bad idea, it’s a better one, definitely do-able, but some thing are design-wrong there – you would need to fetch all the data, even if you are trying to add just a single email address.
It is a quick solution, if you have a low rate emails/product (and you know it will ever be so – which you probably don’t and can’t know).
acf/save_post is a good start, but I think you should store it to product/post meta table – one row per 1 email – they can have same key…
if ($orig_cart_items) {
$field_key = "field_59aff2511ca92";
$value = array();
foreach ($orig_cart_items as $item) {
$value[] = array(
'festival_id' => $item['show_festival_id'],
'edition_id" => $item['show_edition_id'],
'movie_id' => $item['show_movie_id']
);
} // end foreach
update_field($field_key, $value, $post_id);
} // end if
Was looking for something similar and found your post – not sure about the map link out, but on the content not being shown when the marker is clicked, did you include content inside the marker div?
Such as:
<?php
$location = get_field('address');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
<?php echo $address['address']; ?>
<?php echo $address['lat']; ?>
<?php echo $address['lng']; ?>
</div>
</div>
<?php endif; ?>
In order to modify the results of a search you need to modify the search query and this is done using a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I finally found it:
<?php if( have_rows('mdb_artist') ): ?>
<?php while( have_rows('mdb_artist') ): the_row();
$content = get_sub_field('mdb_artist_field');
?>
<a href="<?php echo get_term_link( $content[0]->term_id ); ?>"><?php echo $content[0]->name; ?></a>
<br>
<?php endwhile; ?>
<?php endif; ?>
Querying a relationship in a flex field is the same as querying a relationship in a repeater. See this other recent topic https://support.advancedcustomfields.com/forums/topic/reverse-query-relationship-within-repeater/.
Here’s how I’ve done it. I haven’t tested uploading every file type but the ones I have tried do work. This is using material design icons.
<?php if( have_rows( 'download_list_clone_download_groups' ) ): ?>
<section class="download-lists">
<?php while( have_rows( 'download_list_clone_download_groups' ) ): the_row(); ?>
<article class="download-list">
<h3 class="download-list__title"><?php the_sub_field( 'group_title' ); ?></h3>
<?php if( have_rows( 'downloads' ) ): ?>
<ul>
<?php while( have_rows( 'downloads' ) ): the_row(); ?>
<?php $attachment_id = get_sub_field('file'); ?>
<?php $attachment_url = wp_get_attachment_url($attachment_id); ?>
<?php $pathinfo = pathinfo( get_attached_file($attachment_id)); ?>
<?php $filetype = $pathinfo['extension']; ?>
<?php
switch ($filetype) {
case "pdf":
$fileclass = 'file-pdf';
break;
case "jpg":
case "jpeg":
case "png":
case "svg":
$fileclass = 'file-image';
break;
case "pptx":
case "pptm":
case "ppt":
$fileclass = 'file-powerpoint';
break;
case "xlsx":
case "xlsm":
case "xls":
$fileclass = 'file-excel';
break;
case "doc":
case "docm":
case "docx":
case "odt":
case "rtf":
case "txt":
$fileclass = 'file-word';
break;
default:
$fileclass = 'file-document';
break;
}
?>
<li class="download-list__download"><a href="<?php echo $attachment_url; ?>"><i class="mdi mdi-<?php echo $fileclass; ?>"></i><span><?php the_sub_field( 'download_title' ); ?></span><i class="mdi mdi-download"></i></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</article>
<?php endwhile; ?>
</section>
<?php endif; ?>
You can place the code in your theme’s functions.php file or in a custom plugin. This link explains the differences. I like to use custom plugins, that way, if switching themes, my customizations remain. Using the Pluginception plugin (not necessarily supported by ACF, just a tool I like to suggest to simplify things) allows you to quickly create a plugin for this purpose.
I can’t fully understand your exact use case. Please explain it step by step with full details 😉 This thread might get you going otherwise.
Hi James, I have resolved this…who says ACF can’t handle this sort of thing 😉
I’ll post this on the forum as well just in case anyone wants to see it there. First things first, this is the code I ended up with and I will try to explain it:
<?php $args = array (
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
);
$terms = get_terms( 'category', $args );
foreach ( $terms as $term ) {
echo '<br />';
echo '' . '<img src="' . get_field('category_image', $term->taxonomy . '_' . $term->term_id) . '">';
$post_args = array (
'category_name' => $term->slug,
'posts_per_page' => '-1',
'no_found_rows' => true
);
$query = new WP_Query( $post_args );
while ( $query->have_posts() ) {
$query->the_post(); ?>
<br /><div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
<?php }
} ?>
In its simplest terms this code display a category image field created using ACF and all the pages assigned to the categories. Note these are pages, not posts!
You will see in this code that I have applied <div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
Firstly the hidepost class can be used in CSS to hide all the pages that are now loaded into every page sidebar in the site.
You would just use:
.hidepost {
display:none;
}
in your stylesheet.
Secondly we have a class named: page-id-<?php echo $post->ID; ?>
This pulls in the page id class so you end up with something like page-id-10 which can be used to show or hide the page link something like:
.page-id-10 {
display:block!important;
}
You can of course do this from the stylesheet but I needed to go a step further and have the display of these pages controlled through the admin, which is where ACF came in use again.
I created a repeater field named Sidebar Links with a field in the repeater called Page Link which utilises the Page Link Field Type.
This would normally output a url which was no use in my case as I needed it to output the page ID of the Page Link. So I delved into the ACF plugin itself and edited fields>page_link.php (I have kept a backup of the original).
In page_link.php find around line 603:
// convert $post to permalink
if( is_object($post) ) {
$post = get_permalink( $post );
}
In mine I changed it to:
// convert $post to permalink
if( is_object($post) ) {
echo '.page-id-';
echo $post->ID;
echo '{';
echo 'display:block!important;';
echo '}';
$post = ( '' );
}
In my template I can now use:
<?php
// check if the repeater field has rows of data
if( have_rows('sidebar_links') ):
// loop through the rows of data
while ( have_rows('sidebar_links') ) : the_row();
the_sub_field('page_link');
endwhile;
else :
// no rows found
endif;
?>
which outputs something like .page-id-10 {display:block!important;}
So if I want to control the display of each field dynamically I can now apply this code into the header.php template in the <head></head> section like this:
<style>
<?php
// check if the repeater field has rows of data
if( have_rows('sidebar_links') ):
// loop through the rows of data
while ( have_rows('sidebar_links') ) : the_row();
the_sub_field('page_link');
endwhile;
else :
// no rows found
endif;
?></style>
The above allows me to display the page link if it is selected using the ‘edited’ page link field.
Don’t worry, I sorted it using str_split.
So I entered adaeafagaialam : Europe
Then split the string to create a new array called – $rmcwordwidearray
Then requested that the split be every 2 characters which then gives me the country code.
$rmcwordwide = get_field('rights_management_control_by_europe', $post->ID);
$rmcwordwidearray = str_split($rmcwordwide,2);
It would be nice if ACF could allow separation by pipe or semicolon which then splits and turns them into an array.
I’m happy 🙂
I didn’t recognize when exactly, but after some update conditional logic was broken.
Now I have installed ACF 5.6.1 and conditional logic seems doesn’t work (or at least doesn’t work in some cases).
It was worked before, but now when I set up to show a particular field for certain values of another field, this particular field is never displayed (for any values of the second field). If I set up to show a particular field when the value of another field is not equal to some given value, then my particular field is always displayed (also for any values of the second field).
An interesting detail: sometimes (in rare cases) conditional logic still works. But in most cases in exactly the same situation (exactly with the same fields) it does not work.
P.S.: I do not have any tabs for these specific fields.
Hi @abbasinho
One way to accomplish that is to use the WP function get_queried_object_id
With that you can grab the Post/Page ID of the current page before you start iterating through your loop.
Then, in your loop you can match $nav_item->ID with the returned value of get_queried_object_id then you can apply the class appropriately.
In my testing, I set a variable containing the class name at the top of the loop if there was a match, then reset the variable at the end of the loop. There are a bunch of different ways, but using get_queried_object_id is key.
I realize this is a late reply, but hopefully it’s helpful to you or someone else.
Here’s my test code:
<?php
$current_page_id = get_queried_object_id();
$class = '';
$posts = get_field('relationship', 23);
if( $posts ) {
$content .= '<ul>';
foreach( $posts as $p ) {
if ( $current_page_id == $p->ID ) {
$class = ' style="text-decoration:line-through"';
}
$content .= '<li>';
$content .= '<a href="' . get_permalink( $p->ID ) .'"' . $class . '>' . get_the_title( $p->ID ) . '</a>';
$content .= '</li>';
$class = '';
}
$content .= '</ul>';
}
echo $content;
?>
That Query Monitor plugin was just the trick!
Hello @shonlevi ,
If I understand correctly, perhaps you could have 3 fields…
Field 1: Text box for value such as: EXAMPLE
Field 2: Number… for how many coupons to generate
Field 3: Repeater field for the coupon codes.
Then, you could enter values in the first 2 boxes, and then use the update_field function to generate the codes..
Here’s partial example code for updating a repeater:
<?php
$prefix = get_field('prefix', $post_id);
$number_of_coupons = get_field('number_of_coupons', $post_id);
$repeater_field_key = "field_12345678";
$i = 1;
while ($i <= $number_of_coupons) {
//// use PHP 'rand' or 'shuffle' to generate some random numbers
//// create a 'value' by joining the 'prefix' with a 'random number'
//// put your logic in here to add values to the array
$i++;
}
///// here is how to add one value to the array
$value = array(
array(
"coupon_code" => "Foo"
)
);
update_field( $field_key, $value, $post_id );
?>
Hope that gets you on the right path… clearly there is some work left to do and some testing, but that should get you going!
HI @verena
Thanks very much for your detailed question and providing the version numbers. I appreciate that. The good news is, I was able to duplicate this issue quite easily. However, I am not sure where to begin though to sort out the issue. It is certainly something to bring up to the developers.
I will let the ACF developer know, can you reach out to WPGlobulus?
I will update this thread when I hear back. I can’t guarantee a response time and what sort of priority will be assigned to it. With that said, I will try and see if I can sort anything out on my own time to help move things along.
Talk soon!
EDIT: Ticket submitted to ACF support about the incompatibility
EDIT: ACF Support has been in touch with WPGlobus about this… hopefully it is rectified soon
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.