3

In multi-site is it possible to access entries between sites in the templates. Like a feed? ie. entry "a" on site "A" shows up in an index on site "B", and links back to the entry on site A, not a duplicate of the entry on site "B"?

2 Answers 2

5

Yes it is. Just include the site id of the the site in your query

set siteId = 2
set entries = craft.entries.section('foo').site(siteId).all()

if you want to get the current entry for another site

set currentEntrySiteB = craft.entries.id(entry.id).site(2).one()

If you want to search for disabled entries (for example in CP templates) for other sites you can do

craft.entries.id(entry.id).site(2).enabledForSite(false).one()
3
  • Is it possible to aggregate multiple site's content (for example, news stories) into one query? I don't seem to see a way to include multiple site IDs or handles in an entry query. May 9, 2018 at 11:33
  • 1
    @StephenHamilton No you can't github.com/craftcms/cms/issues/2854 May 9, 2018 at 11:35
  • Thanks @Robin, gutted I didn't see that before getting this far with a build... May 9, 2018 at 12:00
3

I needed to aggregate blog posts entries from ~20 schools (multi-sites) and display them on the main site. Like others, I realised way to0 late in the development that this wasn't an out of the box feature. However by using the Twig array merge filter you can build an array of aggregated entries from your multi-sites.

Then, using the superSort plugin you can subsequently sort the merged array into whatever order you like, chronological in our case.

On this particular multi-site setup, we use a lightswitch showOnMainSite that admin users can switch to the on position to select what entries are pulled through to the main site. In the example below, we use the search filter to filter the entries output accordingly.

Here's a simplified version:

{#-- create an empty array --#}
{% set multiSiteEntries = [] %}

{#-- fetch the multi-site IDS using the getSitesByGroupId method --#}
{% set sites = craft.app.sites.getSitesByGroupId(2) %}

    {#-- loop through the sites in groupId 2 using the site.id, searching for the showOnMainSite lightswitch field --#}
    {% for site in sites %}

        {#-- add retrieved site entries to the multiSiteEntries array --#}
        {% set multiSiteEntries = multiSiteEntries|merge(
            craft.entries().
            siteId(site.id).
            section('blog').
            search('showOnMainSite:1').
            all()) 
        %}

    {% endfor %}

{#-- loop through our populated array of multi-site enries and use the superSort plugin to sort them chronologically --#}
{% for entry in multiSiteEntries|supersort('rsortAs', '{{ object.postDate|date("U") }}') %}

    {#-- our multi-site entries output --#}
    <a href="{{ entry.url }}">
        {{ entry.title }} | {{ entry.postDate|date('jS F Y')}}
    </a>

{% endfor %}

We limit each site output to 3 entries, so overall we are pulling ~60 entries in from 20 multi-sites to use as a landing page for end users to browse at a glance all the news across the 20 schools.

1

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.