您不需要设置新的WP\\U查询,因为您正在循环遍历八达通的数据。在你的情况下,我会做以下事情:
创建一个函数,用于检查是否有具有给定octopus\\u id的帖子
function post_with_octopus_id_exists( $octopus_id ){
//If for some reason the $octopus_id given is empty return false
if( empty( $octopus_id ) ){
return false;
}
//Do a meta query to fetch posts with the given $octopus_id
$args = array(
\'post_type\' => \'employee\',
\'post_status\' => \'any\'
\'meta_query\' => array(
\'relation\' => AND,
array(
\'key\' => \'_octopus_id\',
\'compare\' => \'EXISTS\'
),
array(
\'key\' => \'_octopus_id\',
\'value\' => $octopus_id,
\'compare\' => \'=\',
)
)
);
$the_query = new WP_Query( $args );
//if there are posts with the given $octopus_id return true otherwise return false
return ( $the_query->have_posts() ? true : false );
}
然后,在循环中,可以按如下方式调用函数:
foreach ( $records->data as $record ) {
if( ! post_with_octopus_id_exists( $record->id ) ){
echo "Hello";
}
}
Important: 确保为每篇文章存储的meta\\u密钥命名为“\\u octopus\\u id”