我试图在删除另一个自定义帖子类型时更新一个自定义帖子类型的meta的值。
删除space\\u rental时,我需要更新空间上的一些元值。
我想我不能用delete_post
因为它在元数据被删除后触发,但这对我也不起作用(无论是在垃圾帖还是清空垃圾)。
这里是函数,下面是每个帖子类型的元数据结构。
//When a space_rental is deleted, we release the space slots they had saved
add_action( \'before_delete_post\', \'tps_delete_space_rental\' );
function tps_delete_space_rental( $postid ){
if (get_post_type($postid) != \'space_rental\') {
return;
}
//Loop through the selected slots in the rental
$selectedSlots = get_post_meta($postid, \'selectedSlots\', true);
foreach ($selectedSlots as $slot) {
$spaceSlots = get_post_meta($slot[\'spaceid\'], \'allSlots\', true);
//Loop through the slots in the space and find the ones that have the rentalID set to the deleted post
foreach ($spaceSlots as $sSlot) {
if($postid == $sSlot[\'details\'][\'rentalID\']) {
$sSlot[\'details\'][\'slotStatus\'] = \'open\';
}
}
}
}
“space”post\\u type meta的存储方式如下:
allSlots => array(
\'timestamp\' => \'123456789\', //timestamp representing this slot
\'details\' => array(
\'slotStatus\' => \'open\', //this is either open or filled
\'slotUser\' => 123, //The ID of the user who owns the rental using this slot
\'rentalID\' => 345, //The post_id of the \'space_rental\' that is using this slot
),
);
此“space\\u rental”post\\u类型元数据的存储方式如下:
selectedSlots => array(
\'spaceSlots\' => array(
123456789, 654321987, 9876432654, etc...,// An array of timestamps in this rental
),
\'spaceid\' => 789, //The post_id of the \'space\' where this rental is
);