Multisite - Get current post id

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;

Topic switch-to-blog multisite posts Wordpress

Category Web


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;

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.