我有一个名为room\\u type的post\\u类型和一个分类room\\u设施。我想要他们之间的关系。此外,我想为每个关系room\\u类型和room\\u设施添加一个值。前端如下所示:
该值使用meta\\u key \\u room\\u facility\\u key\\u存储在Posteta中,其中是room\\u facility的id和room\\u type的post\\u id。我认为这是将价值与room\\u类型和room\\u设施联系起来的最简单方法,对吗?
您可以看到我得到了存储在数据库中的值。问题是要拯救他们。这是我的密码。
要添加元盒,请执行以下操作:
function room_facility_custom_meta() {
add_meta_box( \'room_facility_meta\', __( \'Meta Box \', \'room_facility-textdomain\' ), \'room_facility_meta_callback\', \'room_type\' );
}
add_action( \'add_meta_boxes\', \'room_facility_custom_meta\' );
我的回调函数
function room_facility_meta_callback( $post ) {
//nonce_field
wp_nonce_field( basename( __FILE__ ), \'room_facility_nonce\' );
$tax = "room_facility";
$term_facilities = get_terms( $tax, array(\'hide_empty\' => false));
$term_checked_facilities= wp_get_post_terms($post->ID, $tax);
foreach($term_facilities as $term){
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//value from the postmeta
$room_facility_value = get_post_meta($post->ID, $meta_key);
?>
<div class="row">
<label for="checkbox_<?php echo $room_facility_id ?>">
<input type="checkbox" name="checkbox_<?php echo $room_facility_id ?>" id="checkbox_<?php echo $room_facility_id ?>" <?php checked( myArrayContainsWord($term_checked_facilities, $term->term_id)); ?> />
<?php _e( $term->name, \'prfx-textdomain\' )?>
</label>
<input type="text" name="text_<?php echo $room_facility_id ?>" id="text_<?php echo $room_facility_id ?>" value="<?php if ( !empty ( $room_facility_value[0] ) ){ echo $room_facility_value[0]; }?>" />
</div>
<?php
} //end foreach
}
我的保存功能
function room_facility_meta_save( $post_id )
{
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
//if ( !wp_verify_nonce( $_POST[\'room_facility_nonce\'], plugin_basename(__FILE__) )) { <-- THIS DIDN\'T WORK
if(! isset( $_POST[\'room_facility_nonce\'] )){
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
//wp_set_object_terms( 742, array(211), \'room_facility\');
$taxonomy = "room_facility";
$term_facilities = get_terms( $taxonomy, array(\'hide_empty\' => false));
foreach ($term_facilities as $tax => $term)
{
$room_facility_id = $term->term_id;
/* the meta_key for postmeta, note there is the room_facility included,
* since there should be a relationship between post_id (in this case from taxonomy "room_type")
* and "room_facility"
*/
$meta_key = "_room_facility_key_".$room_facility_id;
//update postmeta room_facility_key_<ID>
if( isset( $_POST[ "text_".$room_facility_id] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( $_POST[ "text_".$room_facility_id] ) );
}
//set wp_term_relationships for room_type and room_facility
if( isset( $_POST[ \'checkbox_\'.$room_facility_id ] ) )
{
//wp_set_post_terms( $post_id, $term, $taxonomy); <-- WRONG function
$error_obj = wp_set_object_terms( $post_id, (int)$term->term_id , $taxonomy);
} /*else {
wp_set_post_terms( $post_id, $term, $taxonomy);
}*/
}
}
add_action( \'save_post\', \'room_facility_meta_save\' );
我不确定是否调用了save函数,因为当我执行
echo \'<script>alert("test")</script>\';
什么都没发生。。。
我知道我发布了很多代码,但我真的不知道为什么它不起作用。如果您有问题,请随时提问。