update_post_meta没有保存到wordpress数据库

时间:2015-10-10 23:46:53

标签: wordpress custom-post-type

我正在开发一个插件,以便房地产公司可以使用自定义帖子类型发布他们的属性。我可以在管理菜单上显示自定义帖子类型。但是,当我尝试保存帖子时,我的自定义元框中的信息不会更新以传达更改。我很好奇它是否与save_post挂钩有关,或者它是否是我没有看到的其他东西。下面的代码是我的整个插件。

<?php
/*
* Plugin Name: Real Estate Post Type
*/


register_activation_hook( __FILE__, 'realEstatePostTypeActivation');

function realEstatePostTypeActivation(){
}

function my_admin(){    
    add_meta_box(
    'residential_meta_box',
    'Property Information',
    'displayPostMetaBox',
    'residentialProperty',
    'normal');

    add_meta_box(
    'commercial_meta_box',
    'Property Information',
    'displayPostMetaBox',
    'commercialProperty',
    'normal');  

}

add_action('admin_init', 'my_admin');

function displayPostMetaBox($post){

    $meta = get_post_meta($post->ID);

    $address = !empty($meta['address']) ? $meta['address'] : '';
    $rent = !empty($meta['rent']) ? $meta['rent'] : '';
    $price = !empty($meta['price']) ? $meta['price'] : '';
    $bedrooms = !empty($meta['bedrooms']) ? $meta['bedrooms'] : '';
    $bathrooms = !empty($meta['bathrooms']) ? $meta['bathrooms'] : '';
    $sqft = !empty($meta['sqft']) ? $meta['sqft'] : '';
    $lot = !empty($meta['district']) ? $meta['lot'] : '';
    $smoker = !empty($meta['smoker']) ? $meta['smoker'] : '';
    $basement = !empty($meta['basement']) ? $meta['basement'] : '';
    $finished = !empty($meta['finished']) ? $meta['finished'] : '';
    $garage = !empty($meta['garage']) ? $meta['garage'] : '';
    $cars = !empty($meta['cars']) ? $meta['cars'] : '';
    $pet = !empty($meta['pet']) ? $meta['pet'] : '';
    $style = !empty($meta['style']) ? $meta['style'] : '';
    $notes = !empty($meta['notes']) ? $meta['notes'] : '';

?>

    <label for="address">Address</label><br />
    <input type="text" name="address" style="width: 100%;" value="<?php echo $address; ?>"><br />

    <label for="rent">Rent</label><br />
    <input type="text" name="rent" style="width: 100%;" value="<?php echo $rent; ?>"><br />

    <label for="price">House Price</label><br />
    <input type="text" name="price" style="width: 100%;" value="<?php echo $price; ?>"><br />

    <label for="bedrooms">Bedrooms</label><br />
    <input type="text" name="bedrooms" style="width: 100%;" value="<?php echo $bedrooms; ?>"><br />

    <label for="bathrooms">Bathrooms</label><br />
    <input type="text" name="bathrooms" style="width: 100%;" value="<?php echo $bathrooms; ?>"><br />

    <label for="sqft">Square Feet</label><br />
    <input type="text" name="sqft" style="width: 100%;" value="<?php echo $sqft; ?>"><br />

    <label for="lot">Lot size</label><br />
    <input type="text" name="lot" style="width: 100%;" value="<?php echo $lot; ?>"><br />

    <label for="district">School District</label><br />
    <input type="text" name="district" style="width: 100%;" value="<?php echo $district; ?>"><br />

    <input type="checkbox" size="80" name="basement" value="<?php echo $basement; ?>">
    <span>Has basement?</span><br />

    <input type="checkbox" size="80" name="finished" value="<?php echo $finished; ?>">
    <span>Finished basement?</span><br />

    <input type="checkbox" size="80" name="garage" value="<?php echo $garage; ?>">
    <span>Has garage?</span><br />

    <label for="cars">Number of cars?</label><br />
    <input type="text" name="cars" style="width: 100%;" value="<?php echo $cars; ?>"><br />

    <input type="checkbox" size="80" name="smoker" value="<?php echo $smoker; ?>">
    <span>Allows smokers?</span><br />

    <input type="checkbox" size="80" name="pet" value="<?php echo $pet; ?>">
    <span>Allows pets?</span><br />

    <label for="style">Style of house</label>
    <select name="housestyle">
        <option<?php if($style == "none"){ echo 'selected="select"'; } ?> value="none">Select the style of house.</option>
        <option<?php if($style == "ranch"){ echo 'selected="select"'; } ?> value="ranch">Ranch Style</option>
        <option<?php if($style == "bi-level"){ echo 'selected="select"'; } ?> value="bi-level">Bi-level</option>
        <option<?php if($style == "tri-level"){ echo 'selected="select"'; } ?> value="tri-level">Tri-level</option>
    </select><br />

    <label for="notes">Additional Notes</label><br />
    <textarea name="notes" class="widefat" style="resize: none; height: 200px;"><?php echo $notes; ?></textarea>

    <?php
}

function addPropertyFields($post_id, $post, $update){

if($post->post_type == 'residentialProperty'){

    if(isset($_REQUEST['rent'])){
        update_post_meta($post_id, 'rent', $_REQUEST['rent']);
    }

    if(isset($_REQUEST['price'])){
        update_post_meta($post_id, 'price', $_REQUEST['price']);
    }

    if(isset($_REQUEST['address'])){
        update_post_meta($post_id, 'address', $_REQUEST['address']);
    }

    if(isset($_REQUEST['bedrooms'])){
        update_post_meta($post_id, 'bedrooms', $_REQUEST['bedrooms']);
    }

    if(isset($_REQUEST['bathrooms'])){
        update_post_meta($post_id, 'bathrooms', $_REQUEST['bathrooms']);
    }

    if(isset($_REQUEST['sqft'])){
        update_post_meta($post_id, 'sqft', $_REQUEST['sqft']);
    }

    if(isset($_REQUEST['district'])){
        update_post_meta($post_id, 'district', $_REQUEST['district']);
    }

    if(isset($_REQUEST['smoker'])){
        update_post_meta($post_id, 'smoker', $_REQUEST['smoker']);
    }

    if(isset($_REQUEST['pet'])){
        update_post_meta($post_id, 'pet', $_REQUEST['pet']);
    }

    if(isset($_REQUEST['housestyle'])){
        update_post_meta($post_id, 'housestyle', $_REQUEST['housestyle']);
    }

    if(isset($_REQUEST['notes'])){
        update_post_meta($post_id, 'notes', $_REQUEST['notes']);
    }
    }
}

add_action('save_post', 'addPropertyFields', 99, 3);

function residentialPostType(){
    register_post_type('residentialProperty',
    array( 'labels' => array(
    'name' => __('Residential Properties'), 
    'singular_name' => __('Residential Property'),
    'add_new' => __('New Residential Property'),
    'add_new_item' => __('New Residential Property'),
    'edit' => __('Edit'),
    'edit_item' => __('Edit Property'),
    'new_item' => __('New Property'),
    'view' => __('View'),
    'view_item' => __('View Property'),
    'search_items' => __('Search Properties'),
    'not_found' => __('No Properties Found'),
    'not_found_in_trash' => __('No Properties Found in Trash')),
    'public' => true,
    'menu_position' => 5,
    'hierarchical' => true,
    'supports' => array('thumbnail', 'page-attributes', 'title'),
    'has_archive' => true,
    'rewrite' => array('slugs' => 'residential')
    ));
}

add_action('init', 'residentialPostType');

function commercialPostType(){
    register_post_type('commercialProperty',
    array( 'labels' => array(
    'name' => __('Commercial Properties'), 
    'singular_name' => __('Commercial Property'),
    'add_new' => __('New Commercial Property'),
    'add_new_item' => __('New Commercial Property'),
    'edit' => __('Edit'),
    'edit_item' => __('Edit Property'),
    'new_item' => __('New Property'),
    'view' => __('View'),
    'view_item' => __('View Property'),
    'search_items' => __('Search Properties'),
    'not_found' => __('No Properties Found'),
    'not_found_in_trash' => __('No Properties Found in Trash')),

    'public' => true,
    'menu_position' => 5,
    'hierarchical' => true,
    'supports' => array('thumbnail', 'page-attributes', 'title'),
    'has_archive' => true,
    'rewrite' => array('slugs' => 'commercial')
    ));
}

add_action('init', 'commercialPostType');

?>

非常感谢任何帮助。

这篇文章的标题经过编辑,以确保我所遇到的问题清晰明了。我自己在下面解决了这个问题。希望这对有这个问题的人有价值。

2 个答案:

答案 0 :(得分:0)

首先,你的代码第45行附近有一个错误。没有定义名为lot的索引。

接下来,您尝试在自定义字段中显示的是从数据库中检索的值数组。尝试为其添加索引。

然后,在执行挂钩到save_post操作的函数之前,尝试删除检查post类型的代码。

答案 1 :(得分:0)

该问题属于与“save_post”操作相关联的功能。我没有使用传递给addPropertyFields()的$ post变量,而是使用了全局$ post变量。

function addPropertyFields($post_id){
    global $post;

    if($post->post_type != "property"){
        return;
    }

    add_post_meta($post_id, 'address', $_POST['address']) or update_post_meta($post_id, 'address', $_POST['address'], get_post_meta($post_id, 'address', true));
}

add_action('save_post', 'addPropertyFields');