0

enter image description here

I want to make a meta box (in a custom post type) with two different type of fields:

  • Text
  • and select, (maybe put it in array ?)

and display this meta box with this two fields with a single ID like so:

if ( get_post_meta( $post->ID, 'The_ID', true ) != '' ) {
    echo get_post_meta( $post->ID, 'The_ID', true );
}

Is this possible?

1 Answer 1

2

If you save the contents of the two input fields into one post_meta value you are able to call them at once.

In the save_post action

$text = esc_attr( $_POST['text'] );
$select = esc_attr( $_POST['select'] );

// saves the value as "text select"
$combined = trim( sprintf( '%1$s %2$s', $text, $select ) ); 

update_post_meta( $post_id, 'The_ID', $combined );

Get Post Meta

if ( $the_id = get_post_meta( $post->ID, 'The_ID', true ) )
    echo $the_id;

An other way you can do this is by adding all the information in a serialized array and then save this.

In the save_post action

$text = esc_attr( $_POST['text'] );
$select = esc_attr( $_post['select'] );

$combined = serialize( array(
    'text' => $text,
    'select' => $select
) );

update_post_meta( $post_id, 'The_ID', $combined );

Get Post Meta

if ( $the_id = get_post_meta( $post->ID, 'The_ID', true ) )
    $the_id = unserialize( $the_id );

$the_id will contain an array with the values of text and select.

6
  • Mike, the "text" and "select" must have the same ID and I must to put it in array? I do not understand. I don't know how to put them in array.
    – Blanka
    Commented Feb 18, 2013 at 11:55
  • Oh, you want to add everything in an array and then make it available. I edited my answer for you. Commented Feb 18, 2013 at 13:44
  • I tried to do this as it is in the picture, but it doesn't work. see this link What i am doing wrong?
    – Blanka
    Commented Feb 18, 2013 at 16:43
  • The result is this: see here
    – Blanka
    Commented Feb 18, 2013 at 16:54
  • Could you provide us the whole script you use to display your metabox? Commented Feb 20, 2013 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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