因此,我尝试使用基于一些post数据的复杂数组填充高级自定义字段选择框。我一直试图通过自定义查询和循环返回的帖子来实现这一点。
不幸的是,环路似乎返回了diddly蹲。在我的测试安装中,应该返回10个帖子。然而var_dump 在下面的语句中将返回NULL 10次。背景get_post($post) 循环内部将为每个实例返回第一个结果。
奇怪的是,var_dumping公司$hw_selectbox_query 将返回看似正确的对象,因此实际循环本身没有定义$post 对象我可以运行foreach 循环遍历返回的查询,但这似乎不是正确的方法。
FWIW,当放置在空白的前端页面上时,该函数可以完美运行。
function populate_selectbox_field( $field ){
    $field[\'choices\'] = array();
    $hw_selectbox_args = array(
        post_type => \'custom_post_type\',
        orderby => \'title\',
        order => \'ASC\',
        posts_per_page => -1
    );
    $hw_selectbox_query = new WP_Query($hw_selectbox_args);
    if ( $hw_selectbox_query->have_posts() ) : while ( $hw_selectbox_query->have_posts() ) : $hw_selectbox_query->the_post();
        var_dump($post);
        $field[\'choices\'][$post->ID] = $post->post_title
        // Do more complicated stuff in real life
    endwhile; endif;
    return $field;
}
add_filter(\'acf/load_field/name=destination_node\', \'populate_destination_node_field\');
 
                    最合适的回答,由SO网友:random_user_name 整理而成
                    使用自定义查询时,为了访问$post 信息,您需要参考post 自定义WP\\U查询作为类变量。
因为您将查询构造为:
$hw_selectbox_query = new WP_Query($hw_selectbox_args);
然后您可以访问post 变量$hw_selectbox_query, 像这样:
var_dump($hw_selectbox_query->post);
 这将为您提供构建
$field 大堆