Hossein Kurd
Hossein Kurd

Reputation: 4555

how to add custom post meta to add / update all posts in Wordpress

i need to add some detail to posts, so i have to modify page to add/update post

but i don't know what action i should to use

i want to add two select boxes, admin must select first field and then choose second too

for example fields are :

List One, List Two, List Three, List Four

And Sub Selects :

List One : Item One, Item Two, Item Three, Item Four

List Two : Item One, Item Two, Item Three, Item Four

php Code :

// ============================== 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 .

But no value save on update, $post_id is always null

Upvotes: 0

Views: 4520

Answers (1)

suraj singh
suraj singh

Reputation: 93

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');
    }
}

Upvotes: 6

Related Questions