I have an options page with a repeater, in which you select page links to the custom post type ‘programme’. Each of those posts has a custom field called ‘series title’.
In the page link selection, is it possible to have the value displayed as ‘Series Title: Programme Page Title’? I’ve had a look through the docs for dynamically populating the choices and spent a while googling but I’m still fairly limited in my php abilities so I can’t seem to get it working.
I’ve basically been doing variations of this with no success:
function acf_load_series_names($field) {
$field['choices'] = array();
$args = array('post_type' => 'programme');
$programme_array = get_posts($args);
foreach($programme_array as $post) {
$series = get_field('series_title', $post->ID);
$label = $series . ': ' . $post->post_title;
$field['choices'][$label] = $post->post_title;
}
return $field;
}
add_filter('acf/load_field/name=show_link', 'acf_load_series_names');
Any help is appreciated, thank you!
Hi @soroka
I believe you can use the acf/fields/page_link/result/
filter hook. This hook is not documented yet, but you can use it like this:
add_filter('acf/fields/page_link/result/name=show_link', 'my_acf_modify_page_link_title', 20, 4);
function my_acf_modify_page_link_title( $title, $post, $field, $post_id ){
// get the series title
$series_title = get_field('series_title', $post->ID);
// only set the title if it's found
if($series_title) $title = 'Series Title: ' . $series_title;
// return the title
return $title;
}
Please keep in mind that this will only work for ACF PRO version.
I hope this helps 🙂
Brilliant, worked straight out of the box, thanks so much!