Home › Forums › Backend Issues (wp-admin) › How to dynamically populate repeated with all posts from a custom type?
I have products with possible add-ons.
The add-ons have their own custom post type (post name + ACF image field).
Product pages use a repeater with relational field (post object) to add the add-ons.
But, because there’s a lot of add-ons, and almost all are usually included with each new product, I would like to pre-populate the repeater with all the add-ons, and let the user remove those that don’t apply to the new product.
How can I do that?
Here’s how far I got:
add_filter('acf/load_field/name=addons', 'co_acf_load_default_addons', 10, 3);
function co_acf_load_default_addons($field) {
$field['choices'] = array(1593, 1553, 1588);
return $field;
}
But it’s not there yet… Any suggestions?
let me know if you have any questions
add_filter('acf/load_value/name=add_on_repeater', 'prepopulate_add_on_repeater', 20, 3);
function prepopulate_add_on_repeater($value, $post_id, $field) {
if (!empty($value)) {
// already has a value, do not overwrite
return $value;
}
// do a query to get all posts of the post type
$args = array(
'post_type' => 'add-ons', // your post type
'posts_per_page' => -1, // -1 get all
'fields' => 'ids', // return only list of IDs
);
$query = new WP_Query($args);
if (empty($query->posts)) {
// no posts found
return $value;
}
// initialize value, a repeater has an array value
$value = array();
// loop over posts and built repeater value
foreach ($query->posts as $add_on_post_id) {
// each row of the repeater is a nested array
$value[] = array(
// you must use field key to set value of sub field of each row
'field_XXXXXXX' => $add_on_post_id
);
}
return $value;
}
Thank you so much John!
Just a quick change I made for UI’s sake. Instead of a repeater, I’m using a Post Object field which allows for multiple values, and it returns a Post ID. How does that impact your code?
The code I gave you was for a post object in a repeater. It won’t work without the repeater.
Hi John,
Sadly this won’t work without a repeater. How do I make it work with a Post Object (selector2 I think) field with multiple values instead?
Thank you for all your help!
add_filter('acf/load_value/name=post_object_repeater', 'prepopulate_post_object_with_all_posts', 20, 3);
function prepopulate_post_object_with_all_posts($value, $post_id, $field) {
if (!empty($value)) {
// already has a value, do not overwrite
return $value;
}
// do a query to get all posts of the post type
$args = array(
'post_type' => 'add-ons', // your post type
'posts_per_page' => -1, // -1 get all
'fields' => 'ids', // return only list of IDs
);
$query = new WP_Query($args);
if (empty($query->posts)) {
// no posts found
return $value;
}
$value = $query->posts;
return $value;
}
A king among men. On your weekend too no less! Thank you very much for taking the time to help @John Huebner
You must be logged in to reply to this topic.
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.