• nemci7v

    (@nemci7v)


    I have multiple metabox panels in my write posts. Is it possible to get the names and values of all fields belonging to each metabox?

    In my posts I’m currently using a loop (shown below) that gets the values of all fields but I need to have them in sections based on metaboxes. Is that possible?

    $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
          if ( '_' == $valuet{0} )
          continue;
    
          echo get_post_meta($post->ID, $value, true);
          echo "<br />";
      }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The section will not be saved so you will need to include the section ID in the field name like:
    ‘product_code’, product_brand, product_price
    ‘item_size’, ‘item_color’, item_style

    Then split the value by the ‘_’ to get the section, use a switch statement to set the section.

    This sounds like an ideal scenario for custom post types have a read of the links in the More Information, there are a few posts about them.

    Also there was a talk on these at the London WordPress Meetup

    HTH

    David

    Thread Starter nemci7v

    (@nemci7v)

    Thanks for the reply David! I’m not sure how “post types” is relevant to what I need. I’m working with custom fields in posts. In my functions.php file I’m registering multiple metaboxes using multiple arrays for each set of fields:

    $meta_boxes[] = array(
    	'id' => 'products',
    	'title' => 'Products',
    	'pages' => array('post', 'page', 'link'), // multiple post types, accept custom post types
    	'context' => 'normal', // normal, advanced, side (optional)
    	'priority' => 'high', // high, low (optional)
    	'fields' => array(
    		array(
                            'name' => 'something',
    			'id' => $prefix . 'something',
    			'desc' => '',
    			'type' => 'checkbox'
    		      ),
                     //more arrays for other fields
    	         )
               );
    
    //another metabox array
    
    $meta_boxes[] = array(
    	'id' => 'customers',
    	'title' => 'Customers',
    //etc...

    I’m able to echo all fields in a post using:

    $custom_field_keys = get_post_custom_keys();
    foreach ( $custom_field_keys as $key => $value ) {
    	$valuet = trim($value);
    
    	if ( '_' == $valuet{0} )
    		continue;
    		echo $value . ":<br />";
    		echo get_post_meta($post->ID, $value, true) . "<br/><br/>";
    }

    My goal is to get fields for specific metaboxes. For example get all field values of “products” or “customers”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get custom fields from multiple metaboxes’ is closed to new replies.