I have a multisite.
In the main site, I have a flexible content field.
One of the layouts is ‘block_brands’.
I have a repeater with a dynamically generated select.
The values are retrieved from another site in the network and set as choices.
That works as expected.
There is one minor disadvantage. A post can be selected twice (if I don’t add custom validation). So I wanted to give the relationship field a try.
I need to generate the options for this field, since the post type (brands) is not available in the main site, only in a sub-site, hence why I need to add it dynamically.
I added the field and wrote a function to hook into ‘acf/fields/relationship/query’.
This is the function I wrote, which retrieves the correct items in the left part.
function load_brands( $args, $field, $post_id ) {
if ( get_current_blog_id() != get_products_site_id() ) {
switch_to_blog( get_products_site_id() );
}
$args[ 'post_type' ] = [ 'pc_brand' ];
$args[ 'suppress_filters' ] = false;
if ( get_current_blog_id() != get_products_site_id() ) {
restore_current_blog();
}
return $args;
}
add_filter( 'acf/fields/relationship/query/key=field_5d8e84bf2d6d3', 'load_brands', 5, 3 );
When I then select it, they do get stored, but they don’t show up in the right part, probably because the post type is not recognized within this site.
How can I ‘make them show up’ on the right side ?
You are going to need multiple filters because the query that ACF runs is still being run on the current site.
Please note that most of this is just a guess…. actually, I tried looking for this quickly and could not find a solution, but here are the steps that would need to be taken.
remove_filter()
remove_filter()
I think I get the gist of your ‘thinking’ but the side where you select the posts works perfectly.
So imo I would only need to apply a filter to the right side, with a switch to blog, but couldn’t figure out which filter to use for it….
Do they not appear on the right when selected? Or do they not appear on the right after saving the post?
So, the problem is that when the field is loading after saving the post ID values do not exist on the current site.
When ACF renders the field it does this
// get posts
$posts = acf_get_posts(array(
'post__in' => $field['value'],
'post_type' => $field['post_type']
));
the function acf_get_posts() in api-helpers.php is where you need to look. I’m not sure if you can do anything there. I think that you need to find a way to switch sites before acf renders the field and then switch back after acf renders the field. I would try this hook https://www.advancedcustomfields.com/resources/acf-render_field/, twice, low priority to switch to the main site and high priority to restore the current blog.
again, I don’t know if it will work.
> So, the problem is that when the field is loading after saving the post ID values do not exist on the current site.
When ACF renders the field it does this
That is my ‘suspicion’ indeed…
> again, I don’t know if it will work.
Don’t worry, you’ve given me a possible direction where to look, so I appreciate it.
I’ll post the result when I have some time…
The topic ‘Dynamically generate relationship multisite’ is closed to new replies.
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.