3

I have a multisite on Craft 3 and I was wondering how to get section content from another site? Let's say I'm on a default site and I want to get contact content from site with handle siteA?

Can this be done?

2
  • Of course, just include the siteId in your elementQuery. I'm currently on my smartphone so it's difficult to provide code but it should look like $site = Craft::$app->sites->getSiteByHandle('your-handle'); and in your query you include $elementQuery->siteId = $site->id Oct 17, 2017 at 16:33
  • That's a perfectly good answer - thank you @Anubarak ! I was wondering if this is possible on the frontend using craft.entries.section? I was wondering about this approach: craft.entries.section('sectionHandle').relatedTo(...) - and this is where I'm stuck, because in theory I can pass an array with sourceSite attribute. When I looked in the source code, there's indeed an option to do that. I've a section that's enabled for site with handle "default" and id = 1 I'm querying this like this: craft.entries.section('jobs').relatedTo({ element: craft.entries, sourceSite: 1});
    – Szczups
    Oct 17, 2017 at 17:15

1 Answer 1

4

From a template you could do:

{% set entries = craft.entries.section('jobs').site('siteA').all() %}

In your follow up comment, you're passing craft.entries as an element to relatedTo, but that's just going to be an instance of the ElementQuery class. You need to give it the actual element you're interested in finding the relationship to.

1
  • 2
    Thanks Brad, that's very helpful! I also found another way: {%set entries = craft.entries({ section: 'jobs', siteId: 2}).all() %} Downside is, that I need to retrieve id not use handle.
    – Szczups
    Oct 17, 2017 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.