我有一个非常奇怪的问题,我正在尝试解决:
我有一个查询(用于构建数组),我在插件中的自定义帖子类型上运行该查询。看起来是这样的:
function makePointerQuery(){
//global $post;
$pqargs = array(
\'post_type\' => \'sbap_pointer\',
\'posts_per_page\' => -1
);
$pointer_query = array();
$pp_query = get_posts($pqargs);
if ($pp_query) :
foreach ( $pp_query as $post ) : setup_postdata( $post );
$pointer_query[] = array(
\'id\' => get_post_meta($post->ID, \'_sbap_pointerid_text\', true ),
\'screen\' => get_post_meta($post->ID, \'_sbap_screen_text\', true ),
\'title\' => get_the_title($post->ID),
\'content\' => get_the_content($post->ID)
);
endforeach; endif;
wp_reset_postdata();
return $pointer_query;
}
如果我省略了global$post,我的插件似乎运行得很好,但我收到了以下PHP通知(启用调试):Notice: Trying to get property of non-object in /wp-includes/post-template.php on line 289
但如果我把global$post包括进来,当那个通知消失的时候,一些非常奇怪的事情发生了。每个(“添加新的”)新帖子页面(“post,pages,my custom post type等中的post New.php”)都会从我的第一篇自定义帖子中提取并填写数据(即标题、内容、slug都是用该帖子的数据填写的,而不是像新帖子那样填写空白字段)。我不知道如何消除错误通知,同时仍能像预期的那样使用插件功能(而不会弄乱admin中的每一篇新帖子)。
有什么想法吗?提前感谢!