Query posts/products of another site in a multisite network

Hey there guys hope your doing all good!

I’ve set up an multisite network with a shop on one site and an self build elearning site on the other site. Now I want to list all products bought by the current user on the elearning site. I’ve already tried this approach with the wcm api:

add_filter('bricks/posts/query_vars', function ($query_vars, $settings, $element_id) {


  if ($element_id == 'ildiods') {

    if (is_user_logged_in()) {
      $user_id = get_current_user_id();

       $api_url = 'https://mydomain.com/shop/wp-json/wc/v3/memberships/members?customer=' . $user_id;

      $consumer_key = 'xxxxxxxxx';
      $consumer_secret = 'xxxxxxxxxx'; 

      $response = wp_remote_get($api_url, array(

        'headers' => array(
          'Authorization' => 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret)
        )

      ));


      if (!is_wp_error($response)) {
        $memberships = json_decode(wp_remote_retrieve_body($response));
        $product_ids = array();

        foreach ($memberships as $membership) {

          if (isset($membership->id) && !empty($membership->id)) {
            $product_ids[] = $membership->plan_id;
          }
        }


        if (!empty($product_ids)) {

          $query_vars['post_type'] = 'wc_membership_plan';
          $query_vars['post__in'] = $product_ids;

        }
      }
    }
  }

  return $query_vars;

}, 10, 3);

The correct post Id’s were returned but I gues the query run by bricks did not switch blogs so I do not have any results and the query is empty. So how can I manipulate that the query for this specific element id switches to blog id 2?

Thank you for any help in advance!

Sincerly
KlickKlack

Hey Torben,

did you try to use the switch_to_blog() and restore_current_blog() functions? Never tried it myself but sounds like this is the way to go.

You can find an example here.

Let me know if that helps.

Best,

André

Hey aslotta,

thank you for your reply. Yes I’ve already tried to get it working with your considered functions.

add_filter('bricks/query/run', function($results, $query_obj) {

           // get the post id's
            foreach ($memberships as $membership) {
                if (isset($membership->plan_id) && !empty($membership->plan_id)) {
                    $plan_ids[] = $membership->plan_id;
                }
            }

       if (!empty($plan_ids)) {

                switch_to_blog(2);

                $args = array(
                    'post_type' => 'wc_membership_plan',
                    'post__in' => $plan_ids,
                    'posts_per_page' => -1
                );
                $posts = get_posts($args);

                //restore current blog
                restore_current_blog();

                $results = $posts;
}

But I guess it’s the wrong position to switch the blogs because the query of bricks is executed elsewhere. But I dont know how / where to switch the blog for the bricks query. Is there a hook to manipulate queries and where I can use the blog switch functions?

Did you ever find a solution to this?