1

I'm using WPMU in my project, but I'm facing some problems to get the current post id from all sites. If I run this code, I receive the same post id of current site and not the different post id from all other sites. It's like that switch_to_blog doesn't work. How can I get all ids of current post from all sites?

$sites = get_sites();

/** @var WP_Site $site */
foreach ($sites as $site) {
    if ( $site->archived || $site->spam || $site->deleted ) {
        continue;
    }

    switch_to_blog( $site->blog_id );
    var_dump( get_the_ID() );
}
restore_current_blog();
die;
3
  • I don't understand what you want. you want the ID of the current post ? or you want all posts of a blog ?
    – Kaperto
    Dec 12, 2019 at 10:03
  • @Kaperto I want the ID of current post of all sites Dec 12, 2019 at 10:10
  • a post is associated to 1 site then it cannot be at "all sites".
    – Kaperto
    Dec 12, 2019 at 10:52

2 Answers 2

1

try this

$sites = get_sites();
global $switched;

/** @var WP_Site $site */
foreach ($sites as $site) {
if ( $site->archived || $site->spam || $site->deleted ) {
    continue;
}

switch_to_blog( $site->blog_id );
$all_posts = get_posts('category=-3&numberposts=6&orderby=post_name&order=DSC');
?>
<ul>
<?php foreach($all_posts as $post) : setup_postdata($post);?>
    <li>
        <a href="<?php echo get_page_link($post->ID); ?>" title="<?php echo 
         $post->post_title; ?>"><?php echo $post->post_title; ?></a>
    </li>                                
<?php endforeach ; ?>
</ul>
<?php
}
restore_current_blog();
die;
5
  • same problem, I receive the same post id e not the different post id for each site Dec 12, 2019 at 11:03
  • ok then you need to loop all the post. Dec 12, 2019 at 11:06
  • see the answer now Dec 12, 2019 at 11:09
  • In this way I receive the first six posts of current site Dec 12, 2019 at 11:25
  • this function is working fine in my current wp mu setup Dec 12, 2019 at 11:47
0

use global $post. And use $post->ID

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.