until now, I was saving my custom post meta in post_meta table. and my code was:
function add_book_meta_box() {
add_meta_box(
'book_meta_box',
'book info',
'show_book_meta_box',
'book',
'normal',
'high');
}
add_action('add_meta_boxes', 'add_book_meta_box');
$book_meta_fields = array(
array(
'label'=> 'name',
'desc' => 'name',
'id' => 'publisher',
'type' => 'text'
),
array(
'label'=> 'year',
'desc' => 'year',
'id' => 'period',
'type' => 'text'
),
array(
'label'=> 'poster',
'desc' => 'poster',
'id' => 'poster',
'type' => 'text'
));
function show_book_meta_box() {
global $book_meta_fields, $post;
$featured_fields_book = get_post_meta($post->ID, 'featured_fields_book', true);
echo '<input type="hidden" name="book_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
?>
<?php
foreach ($book_meta_fields as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<div class="row-meta"><label for="'.$field['id'].'">'.$field['label'].'</label>';
switch($field['type']) {
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" placeholder="'.$field['desc'].'">';
break;
}
echo '</div>';
}?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.metabox_submit').click(function(e) {
e.preventDefault();
$('#publish').click();
});
$('#add-row').on('click', function() {
var row = $('.empty-row.screen-reader-text').clone(true);
row.removeClass('empty-row screen-reader-text');
row.insertBefore('#download_link tbody>tr:last');
return false;
});
$('.remove-row').on('click', function() {
$(this).parents('tr').remove();
return false;
});
$('#download_link tbody').sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.sort'
});
});
</script>
<table id="download_link" width="100%">
<thead>
<tr>
<th>-</th>
<th>vol</th>
<th>part</th>
<th><i class="icon_move"></i></th>
</tr>
</thead>
<tbody>
<?php
if ( $featured_fields_book ) :
foreach ( $featured_fields_book as $field ) {
?>
<tr>
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="vol_num[]" value="<?php if($field['vol_num'] != '') echo esc_attr( $field['vol_num'] ); ?>" /></td>
<td><input type="text" class="widefat" name="part_num[]" value="<?php if($field['part_num'] != '') echo esc_attr( $field['part_num'] ); ?>" /></td>
<td><a class="sort"><i class="icon_move"></i></a></td>
</tr>
<?php
}
else :
// show a blank one
?>
<tr>
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="vol_num[]" /></td>
<td><input type="text" class="widefat" name="part_num[]" /></td>
<td><a class="sort"><i class="icon_move"></i></a></td>
</tr>
<?php endif; ?>
<!-- empty hidden one for jQuery -->
<tr class="empty-row screen-reader-text">
<td><a class="button remove-row" href="#">-</a></td>
<td><input type="text" class="widefat" name="vol_num[]" /></td>
<td><input type="text" class="widefat" name="part_num[]" /></td>
<td><a class="sort"><i class="icon_move"></i></a></td>
</tr>
</tbody>
</table>
<p><a id="add-row" class="button button-primary button-large" href="#">add new</a>
</p>
<?php
echo '</div>';
}
function save_book_meta($post_id) {
global $book_meta_fields;
if (!wp_verify_nonce($_POST['book_meta_box_nonce'], basename(__FILE__)))
return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($book_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$old2 = get_post_meta($post_id, 'featured_fields_book', true);
$new = $_POST[$field['id']];
$new2 = array();
$vol_num = $_POST['vol_num'];
$part_num = $_POST['part_num'];
$count = count( $vol_num );
for ( $i = 0; $i < $count; $i++ ) {
if ( $vol_num[$i] != '' ) :
$new2[$i]['vol_num'] = stripslashes( strip_tags( $vol_num[$i] ) );
endif;
if ( $part_num[$i] != '' ) :
$new2[$i]['part_num'] = stripslashes( strip_tags( $part_num[$i] ) );
endif;
}
if ($new && $new != $old || $new2 && $new2 != $old2) {
if($field['id'] == 'poster-book'){
$idposter = uploader_poster($_POST['poster-book']);
set_post_thumbnail($post_id, $idposter);
}
update_post_meta($post_id, $field['id'], $new);
update_post_meta( $post_id, 'featured_fields_book', $new2 );
} elseif ('' == $new && $old || '' == $new2 && $old2) {
delete_post_meta($post_id, $field['id'], $old);
delete_post_meta( $post_id, 'featured_fields_book', $old2 );
}
}
}
add_action('save_post', 'save_book_meta');
?>
It was saving meta right. but now I want to save these meta in new custom table. I have created the table {wp_lib_infometa}:
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$table_name = $wpdb->prefix . 'lib_infometa';
$sql = "CREATE TABLE $table_name (
meta_id INTEGER NOT NULL AUTO_INCREMENT,
post_id TEXT NOT NULL,
publisher TEXT NOT NULL,
period TEXT NOT NULL,
poster TEXT NOT NULL,
vol_num TEXT NOT NULL,
part_num TEXT NOT NULL,
PRIMARY KEY (meta_id)
) $charset_collate;";
dbDelta( $sql );
}
add_action("after_switch_theme", "lib_infometa_create_db");
but i don’t know how to save these meta in this table. my code that has issue is (I was using this code where update_post_meta in last code is. is that right place?):
global $wpdb;
$table_name = "wp_lib_infometa";
$vol_num = $_POST['vol_num'];
$period = $_POST['period'];
$info = array(
'post_id' => $post_id,
'period' => $period,
'vol_num' => $vol_num,
);
$datum = $wpdb->get_results("SELECT * FROM $table_name WHERE post_id= '".$post_id."'");
if($wpdb->num_rows > 0) {
$wpdb->update($table_name, $info, array('post_id' => $post_id));
}
else{
$wpdb->insert($table_name, $info);
}
any one can help please?