我在每篇文章中使用多个位置值,因此它是一个数组(总是)。我的post meta form字段如下所示:
<select name="<?php echo $project_prefix; ?>offer_locations[]" id="<?php echo $project_prefix; ?>offer_locations" required multiple="multiple">
...
...
...
</select>
如果帖子已经存在,但没有任何位置值,那么现有值将出现
null
, 但由于该字段是必需的,[保存时]的新位置值肯定是一个数组,我们应该添加posmeta值。
我使用的条件array_diff()
工作正常。我制作了一个自定义场景并检查了代码,甚至在WordPress中,我也检查了代码(custom code block), 正如你所看到的var_dump()
, echo
和exit()
内联。但是
<?php
function save_post_specifics_meta( $post_id ) {
global $project_prefix;
// verify nonce
if (!isset($_POST[\'post_specifics_nonce\']) || !wp_verify_nonce($_POST[\'post_specifics_nonce\'], basename(__FILE__)))
return $post_id;
// check autosave
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( \'post\' == $_POST[\'post_type\'] && !current_user_can(\'edit_post\', $post_id) )
return $post_id;
//Location meta data
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $post_id );
$new_locations = $_POST["{$project_prefix}post_locations"]; //always an array
var_dump($existing_locations); //for debugging only
var_dump($new_locations); //for debugging only
if( is_array( $existing_locations ) ) {
$new_diff = array_diff( $new_locations, $existing_locations );
$existing_diff = array_diff( $existing_locations, $new_locations );
} else {
$new_diff = $new_locations;
$existing_diff = $existing_locations;
}
var_dump($new_diff); //for debugging only
var_dump($existing_diff); //for debugging only
if( $new_diff ) {
foreach( $new_diff as $new_location ) {
add_post_meta( $post_id, "{$project_prefix}post_locations", $new_location );
echo "New Multiple: ", $new_location, \'<br>\'; //for debugging only
}
}
if( $existing_locations && $existing_diff ) {
if( is_array( $existing_diff ) ) {
foreach ( $existing_diff as $existing_location ) {
delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_location );
echo "Delete Multiple: ", $existing_location, \'<br>\'; //for debugging only
}
} else {
delete_post_meta( $post_id, "{$project_prefix}post_locations", $existing_diff );
echo "Delete Single: ", $existing_diff, \'<br>\'; //for debugging only
}
}
exit(); //for debugging only
}
add_action( \'save_post\', \'save_post_specifics_meta\', 10 );
add_action( \'new_to_publish\', \'save_post_specifics_meta\', 10 );
在保存或更新帖子时,位置元字段按预期执行。但如果数据输入操作符点击预览更改按钮,则
add_post_meta()
正在触发,甚至在真正的条件括号内,并添加重复条目,
I don\'t know how!!! :o
我接着补充道true
到最后一个参数(第#40行),使值唯一,以便我可以停止重复条目。但正在添加true
已停止将任何新值添加到meta_key
即使是在储蓄上。
如何停止在预览更改时添加元数据?
最合适的回答,由SO网友:Mayeenul Islam 整理而成
事实上,我很想得到@ialocin的回答,因为他给了我一个建议。但由于注释字段的代码有点长,我将其作为答案发布在这里。但我愿意接受一个更适合这种情况的答案:
在@ialocin的建议下,我明白了$post_id
预览更改命中不准确,因此$existing_locations
未获取正确的数据库信息。现在我更改了以下行:
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $post_id );
进入以下条件行:
if( $_POST[ \'wp-preview\' ] === \'dopreview\' ) {
//if it\'s a preview hit, grab the actual post\'s data
$actual_post_id = wp_get_post_parent_id( $post_id );
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $actual_post_id );
} else {
//else, go with the actual post
$existing_locations = get_post_custom_values( "{$project_prefix}post_locations", $post_id );
}
在localhost中,一切都正常工作。尽管有时,预览热播会使
preview_id
和
preview_nonce
等等,并且不显示预览,但当它们被设置时,预览就是它应该是的样子。