Support

Account

Home Forums ACF PRO Multisite Main Site Options Page Filter Child Site

Solved

Multisite Main Site Options Page Filter Child Site

  • I have a multisite and am trying to accomplish the following.

    On the main site I have a options page with 3 fields:

    promotion_1 Wysiwyg Editor (the html of the promotion)
    promotion_1_storelist Multiple Select (a list of all sites in the multisite)
    promotion_1_expiration Date Picker (standard date picker)

    I want to be able to add the promotion (promotion_1) to the selected sites chosen in the Multiple Select List (promotion_1_storelist) and have the promotion expire on the promotion_1_expiration date. If the site is not selected in the Multiple Select List (promotion_1_storelist) the promotion is not shown.
    On every child site I add the shortcode “center_promotions1” to display the promotion (using shortcode as not every site gets the promotion so abstracted it out of the template).

    I have the expiration working fine and can pull from the main site and display on a child site. I have 2 issues:
    1) How do I filter the Multiple Select List (promotion_1_storelist) and show the promotion only if the child site is selected?
    2) Is there a way to pull in a list of the sites in the multisite and list them in the Multiple Select List (promotion_1_storelist field_5e0a476fc3745)?

    Below is my code so far. It shows the promotion on every site no matter what sites are selected:

    // Promotions display on a Location page
    function get_promotions1(){
    	 ob_start();
    $main_blog = 1;
    $today1 = date('Ymd');
    $currentsite1 = get_current_blog_id();
    
    switch_to_blog( $main_blog );
    $centerpromotions1 = get_field('promotion_1', 'option');
    $storelist1 = get_field('promotion_1_storelist', 'option');
    $expiredate1 = get_field('promotion_1_expiration', 'option'); 
    
    $args = array(
    	'meta_query' => array(
    	 array(
    		'key' => $storelist1, // name of custom field
    		'value' => '"' . $currentsite1 . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
    		'compare' => 'LIKE'
        ),   
      ),
    );
    $sites = get_site($args);
    
    restore_current_blog();
    
    if($expiredate1 > $today1 && $sites && !is_wp_error($sites)):
    	
    echo '<h4>Promotions:</h4><div class="center-promotions-list">' .  $centerpromotions1  . '</div><p></p>';
    
    endif; 
    
    return ob_get_clean();
    }
    
    add_shortcode('center_promotions1', 'get_promotions1');

    Thank you

  • 
    // load values into store list
    // assumes it is only loaded on blog_id == 1
    // assumes that the field is a select field that allows multiple choices
    add_filter('acf/prepare_field/key=field_5e0a476fc3745', 'load_site_list');
    function load_site_list($field) {
      $sites = get_sites();
      $choices = array();
      if ($sites) {
        foreach ($sites as $site) {
          // not the main site
          if ($site->blog_id == 1) {
            continue;
          }
          switch_to_blog($site->blog_id);
          $choices[$site->blog_id] = get_bloginfo('name');
          restore_current_blog();
        } // end foreach site
      } // end if sites
      $field['choices'] = $choices;
      return $field;
    } // end function
    
    
    // see if current blog is selected
    switch_to_blog( $main_blog );
    $storelist1 = get_field('promotion_1_storelist', 'option');
    restore_current_blog();
    if (in_array(get_current_blog_id(), $storelist1)) {
      // current site is selected
    }
    
  • Thank you. I added the code and it works except I can not do a multi-select. For example if a site is like this:
    Main Site (does not display in list)
    Site 1
    Site 2
    Site 3

    Your code displays the list of sites (which is awesome) but I can only choose 1 site. Is there a way to make the list a multi-select so I can show a promotion on “Site 1” AND “Site 3” but NOT on “Site 2”?

    Also how can I add “Allow Null” as an option.

    Again thank you for the above code. Your support here is awesome.

  • These are choices when creating the select field.

  • I am not following you. This is my select setting:

    The actual form does not allow me to multi-select multiple sites to show the promotion on.

  • I was being dumb and not holding the cmd key when selecting multiple sites 🙂
    Beyond rookie mistake. Thanks for the help and it works great!!

  • I moved the code to production with 161 sites. Only the first 99 sites are listed. Any idea on how to get the other 62 sites to list?

    Thank you

  • You need to set the number argument for get sites, unfortunately, it does not seem to be a way to set it to all by using -1 like in WP_Query(), instead it seems that you need to set it to a number that is higher than the number of the sites you have.

    The reason it’s returning 99 is that the max is 100 and we’re eliminating site 1 (the main site)

    
    $sites = get_sites(array('number' => 500));
    
  • Thank you very much…this works awesomely.

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

The topic ‘Multisite Main Site Options Page Filter Child Site’ is closed to new replies.