Multisite foreach loop returning only one result

Question

I have a subdir multisite setup of 10 sites where I have maintained similar Url structure in all of them for a custom post like partoftheurl-in-country (except mainsite) and for the mainsite it’s partoftheurl only. So far so good, everything works fine.

Now I need to create a custom function that returns an array of all matching urls for the current url across the network. I have done this but returns any one of the site only! can anyone put a flash where I’m going wrong.

 function rel_alternate_network(){
    global $post;
    $slugX = is_main_site() ? $post->post_name : rtrim($post->post_name, '-in-'.strtolower(do_shortcode('[country]')));
    $urls = array();
    foreach(get_sites() as $site){
        if( $site->blog_id == get_current_blog_id()) {continue;}
            switch_to_blog($site->blog_id);
            $slugY = get_post_by_slug($slugX);
            $urls['site'] = $site->path;
            $urls['slug'] = $slug2->post_name;
            restore_current_blog();
    }
    return $urls;
}

Note: get_post_by_slug() is custom function I created that works perfectly alright; there is also a shortcode [country] created that returns the country name in the url.

0
TechieBoy 8 months 2021-12-27T05:37:36-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse