Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer
    
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 Feb 18 '13 at 11:55
    
Oh, you want to add everything in an array and then make it available. I edited my answer for you. –  Mike Madern Feb 18 '13 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 Feb 18 '13 at 16:43
    
The result is this: see here –  Blanka Feb 18 '13 at 16:54
    
Could you provide us the whole script you use to display your metabox? –  Mike Madern Feb 20 '13 at 8:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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