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

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;

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;
Share Improve this question asked Dec 12, 2019 at 9:45 Mirko RapisardaMirko Rapisarda 1111 bronze badge 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 Commented Dec 12, 2019 at 10:03
  • @Kaperto I want the ID of current post of all sites – Mirko Rapisarda Commented Dec 12, 2019 at 10:10
  • a post is associated to 1 site then it cannot be at "all sites". – Kaperto Commented Dec 12, 2019 at 10:52
Add a comment  | 

2 Answers 2

Reset to default 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;

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744913773a4600708.html

相关推荐

  • 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

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信