您应该能够替换此部分:
$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;
用这样的方式:
$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;
默认情况下,
get_sites()
返回最多100个站点。您可以使用各种参数对此进行筛选。
另外,请注意,如果您的多站点网络中有很多站点,或者您的各个站点中有很多帖子,那么您应该考虑将结果缓存在WordPress选项或site\\u选项中。switch_to_blog()
运行成本可能很高。
switch_to_blog()
restore_current_blog()