• Hi everyone,

    I’m working on a WordPress multisite setup and I’m trying to figure out if it’s possible to display posts from another site within the network using the Query Loop block in the Block Editor (Gutenberg).

    For example, from site A, I’d like to show posts published on site B, ideally filtered by post type or category. I know that in PHP this can be achieved with switch_to_blog() (as explained in this helpful guide by Rudrastyh), but I’m not sure how (or if) this could be integrated into a block-based workflow, especially using the Query Loop block.

    I was considering extending the Query Loop block itself, as outlined in the documentation here:
    https://developer.ww.wp.xz.cn/block-editor/how-to-guides/block-tutorial/extending-the-query-loop-block/. But in that case, I’m not sure how to restore the current blog properly after rendering. I assume I’d need a kind of post_render_block hook for cleanup, which doesn’t seem to exist.

    Also, on the Editor side, I’m not sure whether it’s even possible to filter the REST API to fetch posts from another site in the network. Has anyone tried something like this?

    Maybe building a custom block would be easier to manage in this case. I’m open to suggestions.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Untested, but I think it’ll work. You’d need to identify a PHP hook that fires before the actual query is made, and another that fires after the query has been made. In the first, call switch_to_blog() so the query will be run on the desired network site. After the query, call restore_current_blog() to switch back.

    There are a number of possible hooks, I imagine. The trick will be in isolating the specific query loop query from all others so you’re only switching for the correct query.

    @alexandrebuffet did you manage to find a solution for this? I’m trying to do the same thing.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display posts from another site (multisite) with Query Loop block’ is closed to new replies.