1

I have managed to save a custom meta field with multiple inputs to a post. The inputs are going like this:

<input type="text" name="menuitem[1][title]" />
<input type="text" name="menuitem[1][section]" />
<input type="text" name="menuitem[1][price]" />

<input type="text" name="menuitem[2][title]" />
<input type="text" name="menuitem[2][section]" />
<input type="text" name="menuitem[2][price]" />      etc.

And the data are stored in serialized arrays.

The reason I do it this way, is because I want to query the values of this field, related to each other. (e.g. to query posts sorted by menuitem[price] of menuitem[title]).

The problem is that WP-query can only read a single key value and cannot read serialized data.

Is there any better way to store meta keys so they are related to each other?

Any suggestions would be appriciated. Thank you

Silenced
  • 11
  • 2

1 Answers1

0

You have to use foreach

Example:

<?php
$entries = get_post_meta($post->ID, '_your_metabox_group_id', true );

    foreach ( (array) $entries as $key => $entry ) {

    $title = $section = $price = '';

    if ( isset( $entry['_your_title_meta_id'] ) )
    $title = get_post_meta($post->ID, '_your_title_meta_id', true);

    if ( isset( $entry['_your_section_meta_id'] ) )
    $section = get_post_meta($post->ID, '_your_section_meta_id', true);

    if ( isset( $entry['_your_price_meta_id'] ) )
    $price = get_post_meta($post->ID, '_your_price_meta_id', true);

    // your output e.g
    // echo '<span class="some-class">'.$title.'</span>';

}
?>
antonio83
  • 454
  • 3
  • 9
  • I 'm sorry I didnt make it clear. My problem is not how to get the values for each "sub key". My problem is how to store the data in the DB so I can get the posts containing their values. – Silenced Aug 14 '14 at 21:07
  • Ah..ok... why do you want to reinvent the wheel? There are tons of amazing libraries that allow you to create grouped fields custom metaboxes and they are regularly maintained. I personally use cmb_custom_metaboxes you can find it here https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress – antonio83 Aug 14 '14 at 21:13
  • Ah I didnt know it. I ll check this out. Thank you! – Silenced Aug 14 '14 at 21:23