Support

Account

Home Forums ACF PRO Multisite blog list ordered by options page value

Unread

Multisite blog list ordered by options page value

  • I have been trying to create a list of subsite links on my main site using an ACF options page value get_field( 'nn_city_name', 'option' ); as the text for each link. I can get a working list, but my attempts to sort the list by nn_city_name all result in errors.

    Here’s my function:

    function x_list_sites() {
      $subsites = get_sites(
        array(
          'site__not_in' => '1',
          // 'orderby' => 'domain',
        )
      );
      if ( !empty( $subsites ) ) {
        echo '<h3>Nerd Nite Cities</h3>';
        echo '<ul class="subsites">';
        foreach ( $subsites as $subsite ) {
          $subsite_id = get_object_vars( $subsite )[ 'blog_id' ];
          switch_to_blog( $subsite_id );
          $subsite_name = get_field( 'nn_city_name', 'option' );
          $subsite_link = get_blog_details( $subsite_id )->siteurl;
          echo '<li class="site-' . $subsite_id . '"><a href="' . $subsite_link . '">' . $subsite_name . '</a></li>';
        }
        restore_current_blog();
        echo '</ul>';
      }
    }

    Here’s a test page that puts the list in the sidebar: https://nnalphastaging.wpengine.com/testing-1-2-3/

    Notes:

    1. I feel like I should be able to do some kind of array_push() to add nn_city_name to $subsites so that I can sort by nn_city_name, but my attempts have failed.
    2. 'orderby' => ‘domain’ gets most of the subsites into correct order, but you run into problems with a subdomain like “nyc” for “New York”.

    Thanks in advance!

Viewing 1 post (of 1 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.