Support

Account

Home Forums Bug Reports Repeater field in multisite with php import

Solved

Repeater field in multisite with php import

  • This issue has been mentioned for a few years around the web and I can now confirm it exists and the circumstances to recreate.

    Description

    On a multisite setup, when you have a repeater field which is defined through ACF fields within functions.php, then using get_field to try and retrieve this data on another one of the blogs will not return the repeater data but only a string of ‘1’ (ie: it will return the same result as get_post_meta() and not be interpreted as a repeater).

    To reproduce

    1. Create multisite network. You now have a MAIN site
    2. Create 2 new sites, SITE A and SITE B
    3. Set up a repeater field through WordPress on SITE A
    4. Add dummy data to a post/page on SITE A through WordPress
    5. Export php code for field group from SITE A
    6. Add field group php code to SITE B functions.php
    7. Add dummy data to post/page on SITE B through WordPress
    8. Switch to MAIN SITE
    9. Now try to use get_field() to print data from each site onto the MAIN SITE using switch_to_blog() functions
    10. Repeater data from SITE A will be returned correctly in array
    11. Repeater data from SITE B not be returned (a string of ‘1’ will instead’

    To fix the issue, export field group data in JSON format from SITE A and import into SITE B through WordPress. Now everything works correctly. So the issue specifically has to do with the fieldgroup existing in functions.php.

    This becomes problematic on ‘automated’ multisite networks, where the field groups need to exist in functions.php because it is not practical/too techinical for site users to import JSON field group data on the creation of each new site. If the field groups cannot be integrated and fully functionally through functions.php the plugin becomes inadequate for complex use in multisites.

  • If the PHP code is specifically for “SITE B” and the PHP code is not run when the “MAIN SITE” or “SITE A” is being viewed, then the field group does not exist, it’s not set up. Since the field group and the fields do not exist ACF does not know that the field you are trying to get is a repeater field, when ACF gets the field key for the field you are getting there is no field that matches that key. Since ACF can’t figure out what to do with the value it assumes that the value is a simple text value and returns the value that is actually stored for the postmeta of the repeater field name. Using swith_to_blog() does not run PHP code specific to that blog, it just changes what table in the database that WP gets data from.

    Hopefully that helps those that are looking understand why the issue exists. It’s not something that can be fixed without doing a little work.

    If you want to be able to access data in fields for all sites on a multisite install then the fields must be defined in all sites. This can be accomplished in several ways, the OP gave one way.

    1) You could create a plugin that is activated for all sites or even a must use plugin and in that plugin you set up the field groups using PHP.

    2) You could place the JSON files in the “MAIN SITE” acf-json folder and define a second load point

    3) You could create a plugin that includes JSON files and set a second load point to a folder inside the plugin.

    I’m sure there are other possibilities.

  • Ok, I now understand the issue properly.

    The way my multisite is set up is that the MAIN SITE uses a custom theme, and the sub sites use a child theme of that theme. For this reason I don’t think I can simply use the PHP code specifying the fields for the sub sites in the MAIN SITE’s functions.php, or load up the fields through JSON in the MAIN SITE, as I don’t actually want the fields active on the parent theme.

    I will have to come up with something else now that I better understand the issue, but I see that this is not actually a bug so it is resolved.


    @hube2
    thank you very much for your help and gratz on solving 666 o_o

  • Actually, yes you can. https://codex.wordpress.org/Function_Reference/is_child_theme. In the parent themes functions.php, check to see if the theme is a child theme, if it is then either add the field groups using PHP or add a new load post for the JSON files.

  • I’m glad I found this post… I had the exact same problem with the Gallery Field.

    I personally decided to just add the php to all of my themes’ function.php & then “hide” the fields (in my case an entire option page called Site Content Settings) on my main site, as this was the only theme that didn’t require these fields.

    add_action( 'admin_menu', 'my_remove_menu_pages', 999  );
    function my_remove_menu_pages() {
        remove_menu_page('site-content-settings');   
    } 
    

    Bit messy, but everything works again. No one will every know 🙂

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

The topic ‘Repeater field in multisite with php import’ is closed to new replies.