如何添加自定义post meta来添加/更新Wordpress中的所有帖子

时间:2016-06-05 11:49:51

标签: php wordpress post-meta

我需要在帖子中添加一些细节,所以我必须修改页面以添加/更新帖子

但我不知道应该采取什么行动

我想添加两个选择框,管理员必须选择第一个字段,然后再选择第二个

例如字段是:

  

列表一,列表二,列表三,列表四

和Sub选择:

  

列表一:第一项,第二项,第三项,第四项

     

清单二:第一项,第二项,第三项,第四项

php代码:

// ============================== Add Province And City ...
add_action( 'add_meta_boxes', 'selectIranProvinceCity' );
add_action( 'save_post', 'iranProvinceCitySavePostData' );
function selectIranProvinceCity(){

    // require_once( 'iran_province_city.php' );

}
function iranProvinceCitySavePostData(){
    // Save Province And City
    $province = '';
    $city = '';
    if( isset( $_POST['state'] ) ) {
        $province = $_POST['state'];
    }
    if( isset( $_POST['city'] ) ) {
        $city = $_POST['city'];
    }
    $post_id = get_the_ID();
    if( !add_post_meta($post_id, 'province', $province, true ) ) {
        update_post_meta($post_id, 'province', $province, true );
    }
    if( !add_post_meta($post_id, 'city', $city, true ) ) {
        update_post_meta($post_id, 'city', $city, true );
    }
}
// ============================== Add Province And City .

但是没有值保存更新,$post_id始终为空

1 个答案:

答案 0 :(得分:5)

add_action('init', 'update_all_templates_to_new');
function update_all_templates_to_new()
{
    $args = array(
        'posts_per_page'   => -1,
        'post_type'        => 'product',
        'suppress_filters' => true 
    );
    $posts_array = get_posts( $args );
    foreach($posts_array as $post_array)
    {
        update_post_meta($post_array->ID, '_use_new_product_template', '1');
    }
}
相关问题