Support

Account

Home Forums ACF PRO Display User Data with MultiSite

Solving

Display User Data with MultiSite

    • juiceex

    • August 25, 2016 at 2:23 am

    I’ve been able to use the “Switch_To_Blog” command work for basic ACF text and attachment fields, but my job list field is a repeater field and I am struggling trying to call the data into my secondary sites.

    So below, I start off my switching to blog 1 and defining some variables, and then go into displaying the data.

    <?php switch_to_blog(1); 
    $jobs_list = get_field('job_titles', 'user_' . $user_db .'');
    ?>
    <?php restore_current_blog(); ?>
    	  
    
    <?php
      if( $jobs_list ) {
         $num_rows = 0;
    	 while ( have_rows('job_titles', 'user_' . $user_db .'') ) : the_row(); // do I need to convert "have_rows()" to a variable?? 
    	 $num_rows++;
    	 endwhile;
    							
    	 while ( have_rows('job_titles', 'user_' . $user_db .'') ) : the_row(); // do I need to convert "have_rows()" to a variable?? 
    	 $num_rows--;
    	 echo '<span>'. get_sub_field('job_title') .'</span>'; // do I need to convert "get_sub_field()" to a variable?? 
    	 if ( $num_rows == 0 ) { echo ''; }
    	 else { echo ', '; }
    	 endwhile;
    }
    ?>

    This code currently works on my main blog, but fails to display anything on my secondary sites.

    I made comments in the code where I am assuming I need to convert these into variables and define then in the space I switched to blog 1…. but I am not sure how to write it.

    Any help would be greatly appreciated!
    Thanks!

  • There are 2 choices.

    The first one is to not use the have_rows loop. You already have all of the data from the repeater, it an array, you just need to loop through it differently, see below. The second one is to not do restore_current_blog(); until after you’re done with the loop.

    
    <?php 
      switch_to_blog(1); 
      $jobs_list = get_field('job_titles', 'user_' . $user_db .'');
      // show me what's in $job_list
      echo '<pre>'; print_r($jobs_list); echo '</pre>';
      php restore_current_blog();
    ?>
    

    you can get the number of rows like this

    
    $num_rows = count($jobs_list);
    

    you can loop through them like this

    
    foreach ($jobs_list as $row) {
      echo <span>'. $row['job_title'] .'</span>'
    }
    
  • I think I have the same issue. I have a repeater in the options area of site #12 and I can’t access it from site #2.

    
    // on site 12, works, displays all the data in the repeater
    print_r( get_field( 'featured_items', 'options' ) );
    
    
    // on site 2, only shows "6", the number of items in the repeater
    switch_to_blog( 12 );
    print_r( get_field( 'featured_items', 'options' ) );
    restore_current_blog();
    
    
    // on site 2, this is a text field, this works
    switch_to_blog( 12 );
    print_r( get_field( 'company_name_long', 'options' ) );
    restore_current_blog();
    
  • This issue is caused by the field group and fields not being defined on site 2. Because the field group and more importantly the field is not defined for the site where you’re attempting to get the values ACF does not know that it is a repeater field.

    All field groups and fields need to be defined on all sites where the fields will be used.

  • Thanks John that tip solved it for me!

    In my case I’m placing the field groups on my production server with the json files and generating them from my local install. This way the client can’t edit them and I manage the fields through a git repo.

    So to get around this problem I’m making a copy of the field group json on site 12 (theme 12) and giving it to site 2 (theme 2). And then I change “active” to 0, this way the field is not displaying on site 2 but it is registered, and that seems to be enough for ACF to load the field contents from site 12 again! The one thing I have to aware of now is that I need to keep these files in sync when changing f.e. a label, and re-apply the “active” 0 difference.

  • You could store the JSON files in a single location and instead define a load point for local json in the theme, that way you don’t need to have multiple copies of the field groups in each theme. https://www.advancedcustomfields.com/resources/local-json/#loading-explained

    • gb3k

    • January 8, 2019 at 7:00 am

    Hi John,
    I am trying to implement this.
    I have a main site and I am trying to get custom fields in that site posts.
    Is the same if I use the Export PHP and I place it in functions PHP that if I do it via json as you recommended?

    Best,

  • Yes, if you define the field groups in PHP for the site in question then it will work.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.