Support

Account

Home Forums ACF PRO Dynamically generate relationship multisite

Solving

Dynamically generate relationship multisite

  • 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.

    1. Your current filter needs to add a pre_get_posts action
    2. In the pre_get_posts action you switch blogs if you need to and alter the query arguments
    3. The pre_get_post action needs to remove itself using remove_filter()
    4. The pre_get_posts action also needs to add an action that will happen after the query is run…. I could not find the proper hook to use here.
    5. In the action that runs after the query gets the posts you need to switch back to the current blog if needed
    6. The final action also needs to remove itself using 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?

  • They don’t show after save. They show when I select them.

  • 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…

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Dynamically generate relationship multisite’ is closed to new replies.