You should be able to replace this section:
$posts = get_posts( 'numberposts=-1&post_status=publish&post_type=questionnaire' );
$choices = array();
$choices[] = array("text" => "Select Questionnaire", "value" => "");
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->choices = $choices;
with something like this:
$choices = array();
$sites = get_sites();
$choices[] = array("text" => "Select Questionnaire", "value" => "");
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$posts = get_posts( 'numberposts=-1&post_status=publish&post_type=questionnaire' );
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
restore_current_blog();
}
$field->choices = $choices;
By default, get_sites()
returns up to 100 sites. You can filter this with various arguments.
Also, note that if you have a lot of sites in your Multisite network, or a lot of posts in your various sites, you should consider caching your results in a WordPress option or maybe site_option. switch_to_blog()
can be expensive to run.