下一段代码给了我一个错误。init 钩子很早就可以访问:array($post->ID)。如果我使用 'wp' 钩子,则 'var_dump($all_dash_for);' 总是返回'null'。
我将此代码用于管理区域中的插件元框。当我编辑自定义帖子类型时,我想排除查询的帖子 ID。有任何想法吗?谢谢
function dash_for() {
global $all_dash_for, $post;
$all_dash_for = array();
$args = array(
'post_type' => 'sliding_panel',
'post__not_in' => array($post->ID)
);
// Query the posts:
$panel_for_query = new WP_Query($args);
while ($panel_for_query->have_posts()) : $panel_for_query->the_post();
$dash_for = get_post_custom_values('_dash_for');
if(!empty($dash_for)){
array_push($all_dash_for, $dash_for[0]);
}
endwhile;
// Reset Post Data
wp_reset_postdata();
if ( empty( $all_dash_for ) ) {
array_push($all_dash_for, 'empty');
}
var_dump($all_dash_for);
}
add_action( 'init', 'dash_for' );