0

I'm trying to get some posts from another blog but it won't work if I want them from a specific category.

switch_to_blog(1);
$replays = get_posts([
    'post_type' => 'post',
    'numberposts' => 10,
    'category' => 1
]);
// Whatever ...
restore_current_blog();

It works as intended if I don't need a specific category though.

Is it the normal behavior ? Do I need to write a custom SQL query ?

4
  • 2
    how are you getting the ID of the specific category? Note that a category might not have the same ID on the other site, even if it shares the same slug/name, because they aren't the same category, they're just categories on separate sites that happen to share a name
    – Tom J Nowell
    Commented Nov 13 at 13:16
  • It's the ID of the specific category from the blog I want to get the posts from. I'm aware that they are not the same. Also when I try to get the said category it works perfectly : switch_to_blog(1); get_category(1);
    – Ben
    Commented Nov 13 at 13:55
  • 1
    I tested this on a fresh install of WP 6.7 and had no issue. So no, not normal behavior, and a custom SQL query shouldn't be needed. There's likely something interfering (from theme or plugin). You may be able to use a tool like Query Monitor to review the queries and see if it's being altered.
    – Caleb
    Commented Nov 13 at 15:10
  • Sometimes websites in the same multisite network might have different IDs for categories so you might want to try get categories by their names or slugs first Commented Nov 13 at 16:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.