$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>multisite - How to get all pages on specific blog after switch_to_blog?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

multisite - How to get all pages on specific blog after switch_to_blog?

matteradmin9PV0评论

I am attempting to loop through all sites on my multisite network blog. However, when I attempt to use get_pages it ignores the fact that the blog has switched via switch_to_blog.

$sites = wp_get_sites( array( 'limit' => 1000 ) );
foreach ( $sites as $site ) {
    $blog_id = intval( $site['blog_id'] );
    if ( $blog_id < 2 ) {
        continue;
    }
    switch_to_blog( $blog_id );
    $pages = get_pages( array(
        'sort_order' => 'asc',
        'sort_column' => 'ID',
        'post_type' => 'page',
        'post_status' => 'publish',
    ) );
    echo 'Blog ID: ' . get_current_blog_id() . ' | Total Pages: ' . count ( $pages ) . '<br>';
    // foreach( $pages as $page ) {
    //  echo 'Blog ID: ' . $blog_id . ' | Post ID: ' . $page->ID . '<br>';
    // }
    restore_current_blog();
}

Output:

Blog ID: 2  | Total Pages: 71
Blog ID: 3  | Total Pages: 71
Blog ID: 4  | Total Pages: 71
Blog ID: 5  | Total Pages: 71
Blog ID: 6  | Total Pages: 71
Blog ID: 7  | Total Pages: 71
Blog ID: 8  | Total Pages: 71
Blog ID: 9  | Total Pages: 71
Blog ID: 10 | Total Pages: 71
Blog ID: 11 | Total Pages: 71
Blog ID: 12 | Total Pages: 71
Blog ID: 13 | Total Pages: 71
Blog ID: 14 | Total Pages: 71
Blog ID: 15 | Total Pages: 71
Blog ID: 16 | Total Pages: 71
Blog ID: 17 | Total Pages: 71
Blog ID: 18 | Total Pages: 71
Blog ID: 19 | Total Pages: 71
Blog ID: 20 | Total Pages: 71

The script above will var_dump the same $pages through out the entire loop, regardless of which blog it switches to. What exactly am I doing wrong, and is there a way to accomplish what I'm attempting to do?

I am attempting to loop through all sites on my multisite network blog. However, when I attempt to use get_pages it ignores the fact that the blog has switched via switch_to_blog.

$sites = wp_get_sites( array( 'limit' => 1000 ) );
foreach ( $sites as $site ) {
    $blog_id = intval( $site['blog_id'] );
    if ( $blog_id < 2 ) {
        continue;
    }
    switch_to_blog( $blog_id );
    $pages = get_pages( array(
        'sort_order' => 'asc',
        'sort_column' => 'ID',
        'post_type' => 'page',
        'post_status' => 'publish',
    ) );
    echo 'Blog ID: ' . get_current_blog_id() . ' | Total Pages: ' . count ( $pages ) . '<br>';
    // foreach( $pages as $page ) {
    //  echo 'Blog ID: ' . $blog_id . ' | Post ID: ' . $page->ID . '<br>';
    // }
    restore_current_blog();
}

Output:

Blog ID: 2  | Total Pages: 71
Blog ID: 3  | Total Pages: 71
Blog ID: 4  | Total Pages: 71
Blog ID: 5  | Total Pages: 71
Blog ID: 6  | Total Pages: 71
Blog ID: 7  | Total Pages: 71
Blog ID: 8  | Total Pages: 71
Blog ID: 9  | Total Pages: 71
Blog ID: 10 | Total Pages: 71
Blog ID: 11 | Total Pages: 71
Blog ID: 12 | Total Pages: 71
Blog ID: 13 | Total Pages: 71
Blog ID: 14 | Total Pages: 71
Blog ID: 15 | Total Pages: 71
Blog ID: 16 | Total Pages: 71
Blog ID: 17 | Total Pages: 71
Blog ID: 18 | Total Pages: 71
Blog ID: 19 | Total Pages: 71
Blog ID: 20 | Total Pages: 71

The script above will var_dump the same $pages through out the entire loop, regardless of which blog it switches to. What exactly am I doing wrong, and is there a way to accomplish what I'm attempting to do?

Share Improve this question edited Jul 27, 2016 at 20:46 Josh Foskett asked Jul 27, 2016 at 20:06 Josh FoskettJosh Foskett 1676 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try using get_posts instead of get_pages

$pages = get_posts( array(
        'order' => 'ASC',
        'orderby' => 'ID',
        'post_type' => 'page',
        'post_status' => 'publish',
    ) );

Question is old, but for anyone with similar issue, this should work for you:

<?php

$sites = get_sites([
    'number' => 9999,
]);

foreach ($sites as $site) {
    switch_to_blog($site->blog_id);

    $pages = get_pages();
    echo count($pages);

    restore_current_blog();
}

wp_get_sites was deprecated in 4.6 in favor of get_sites

Post a comment

comment list (0)

  1. No comments so far