0

I already read this question but it doesn't help me. I create metabox to connect 2 custom post type( 'author' and 'book' ) so that when user want to add a new book can choose author from metabox.

when I want to save authors of the book to db just the last author stores in db (wp_postmeta table).

here is my code

my metabox

add_action('admin_init', 'p2p2_add_author_metabox');

add_action('admin_init', 'p2p2_add_author_metabox');

function p2p2_add_author_metabox(){
    add_meta_box( 
        'book_author', 
        __('نویسنده کتاب', 'bandpress'), 
        'p2p2_book_author_metabox', 
        'book', 
        'side', 
        'default', 
        array( 'id' => 'p2p2_author') 
    );
}

show in book custom post type

function p2p2_book_author_metabox($post, $args) {
 wp_nonce_field( plugin_basename( __FILE__ ), 'p2p2_book_author_nonce' );
 $author_id = get_post_meta($post->ID, 'p2p2_book_author', true);
 echo "<p>انتخاب نویسنده کتاب</p>";
  // Query the authors here
  $query = new WP_Query( 'post_type=author' );
   while ( $query->have_posts() ) {
        $query->the_post();
        $id = get_the_ID();
        $input_check = "";
        $cooaprate=get_field('auth-trans'); 
       if(($cooaprate[0]=='نویسنده')){



         $input_check="checked($author_id[$id][0],'yes')";
          echo '<input name="save-author-to-book[]" type="checkbox"' . $input_check . ' value=' . $id ; 

            if (get_post_meta( $post_id, $key,$id )) {
            echo " checked";

          }


          echo '>' . get_the_title() . '</br>';







        }

}
}

save in database

add_action( 'save_post', 'p2p2_save_author_metabox' );
    function p2p2_save_author_metabox( $post_id) {
       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( !isset( $_POST['p2p2_book_author_nonce'] ) )
        return;
    if ( !wp_verify_nonce( $_POST['p2p2_book_author_nonce'], plugin_basename( __FILE__ ) ) )
        return;
    if (isset($_POST['save-author-to-book'])) {




    $key = 'save-author-to-book';
    $values = $_POST["save-author-to-book"];

    foreach ($values as $value) {


    if ( get_post_meta( $post_id, $key, true ) ) { // If the custom field already has a value


        update_post_meta( $post_id, $key, $value);
    } else { // If the custom field doesn't have a value


        add_post_meta( $post_id, $key, $value );
    }
    if ( !$value ) delete_post_meta( $post_id, $key ); // Delete if blank
}
 }


}

I know the problem is come from if ( get_post_meta( $post_id, $key, true ) ) it overwrite same records but I really don't know how can I fix it.

any idea would be appreciated.

1

Here you can just save the info in array and save that array in your post meta.

some thing like this will work;

$key = 'save-author-to-book';
$values_to_save = array();
$new_values = $_POST["save-author-to-book"];
$existing_values = get_post_meta( $post_id, $key, true ) ;

if(!empty($existing_values)){
   foreach($existing_values as $existing_value){
      $values_to_save[] = $existing_value;
   }
}


if(!empty($new_values)){
   foreach($new_values as $new_value ){
      $values_to_save[] = $new_value ;
   }
}

 update_post_meta( $post_id, $key, $values_to_save ); // you don't need to use if condition as  update post meta will take care

And here is how you can display the information stored in an array;

$key = 'save-author-to-book';
$values = get_post_meta( $post_id, $key, true );

foreach($values as $value){
   echo $value . '<br>';
}
  • ok it works fine and stores in database. but it store as an array and I don't know how can I display chosen values in my custom filed – mkafiyan Aug 24 '16 at 8:18
  • first if wasn't needed. it works fine in database now but still don't have any idea to how can display checked values in metabox. – mkafiyan Aug 24 '16 at 8:37
  • Updated code on how you can display those values in front end. – Mohammad Mursaleen Aug 24 '16 at 10:14
  • I update it. you can check – mkafiyan Aug 24 '16 at 10:17
  • actually I want to check all input which checked not show them on the post. I try to do it with this part echo '<input name="save-author-to-book[]" type="checkbox"' . $input_check . ' value=' . $id ; if (get_post_meta( $post_id, $key,$id )) { echo " checked"; } echo '>' . get_the_title() . '</br>'; – mkafiyan Aug 24 '16 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.