Title: Custom Field Array
Last modified: August 19, 2016

---

# Custom Field Array

 *  [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/)
 * Hi,
 * I am pretty new to php and I am sure that there is a something glaring that I
   am missing, but I am hoping someone can help me.
 * I have to custom fields that each contain multiple values and I am trying to 
   get them to display in sequence. I am using the code:
 *     ```
       <?php
   
       $m_venue = get_post_meta($post->ID, "monday_venue_name", false);
       $m_event = get_post_meta($post->ID, "monday_event_description", false);
   
           foreach ( $m_venue as $m_venue) {
   
                   if ($m_venue){
   
                       echo '<div>'.$m_venue.'</div>';
   
                       echo '<div>'.$m_event.'</div>';
   
                   }        
   
               }
   
       ?>
       ```
   
 * I can get the $m_venue to display correctly, but $m_event simply comes out as“
   Array”.
 * I believe I need to break up the array into its individual vales, but I am having
   trouble figuring out how to do that.
 * If anyone has some suggestions about how I might do this, or where I might turn
   for more information, I would greatly appreciate it.
 * Thank you very much!

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688087)
 * You will need some way to connect each $m_venue to its own $m_event. I do not
   see anything in the code shown that indicates a link between the two.
 * If these are the only two data items you have, it might be simpler to put them
   both in a single custom field with a separator between them. Then explode them
   out to display them separately.
 * Otherwise, you will need to put some key field in each of your values that can
   be extracted and used to look up the corresponding values.
 *  Thread Starter [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688107)
 * Hey vtxyzzy!
 * Thank you so much for your response!
 * I see your point and it makes perfect sense!
 * I must admit that I am not entirely sure how I might assign a link value to each
   of the fields automatically, but I will investigate!
 * I was also exploring other options for getting the result I want and I came up
   with this code:
    ‘<?php
 * $m_venue = get_post_meta($post->ID, “monday_venue_name”, false);
    $m_event = 
   get_post_meta($post->ID, “monday_event_description”, false);
 *  for ( $i = 0; $i < count($m_venue); $i++) {
 *  echo ‘<div>’.$m_venue[$i].'</div>’;
 *  echo ‘<div>’.$m_event[$i].'</div>’;
 * }
 * ?>’
 * Which does in fact return the value of each corresponding field, however the 
   two are not linked. In other words, the $m_venue and $m_event values I was to
   have “grouped” together are not grouping.
 * I believe this is almost exactly the point you made as well.
 * I was thinking of using reset() to make sure that each array will start at 0,
   but I have not been able to make that work.
 * Thank you again, and if you have any other suggestions I would love to hear them!
 * Michael
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688112)
 * AFAIK, there is no way to automatically assign a link key to each value. That
   is something you will have to do as you enter each custom field.
 * Using reset() will not work because the arrays are not guaranteed to be in corresponding
   order to begin with.
 * You will either need to put all values that go together in one custom field, 
   say comma-separated, or else add in a key to each pair.
 * Since each set will be unique to a post, this should be fairly easy. For example,
   you could just use a zero-padded number like this:
 *     ```
       Custom Field               Value
       ------------------------   ---------------------------
       monday_venue_name          001,the first venue
       monday_event_description   001,the first description
       monday_venue_name          002,the second venue
       monday_event_description   002,the second description
       ```
   
 * Then, index the event array and get the values by key in the display loop:
 *     ```
       foreach ($m_event as $event) {
          list($key, $value) = explode(',',$event);
          $indexed_events[$key] = $value;
       }
       foreach ( $m_venue as $each_venue) {
          list($key,$venue) = explode(',',$each_venue);
          echo '<div>'.$venue.'</div>';
          echo '<div>';
          if ($description = $indexed_event[$key]) {
             echo $description;
          } else {
             echo '&nbsp';
          }
          echo '</div>';
       }
       ```
   
 *  Thread Starter [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688119)
 * Thank you so much!
 * I was toying with the idea of combining the two arrays into one array but that
   does not work either.
 * I was hoping to keep the entry fields separate on the user end rather than comma
   separated, so I will have to design a key for each pair.
 * Thank you so much for all of your help!
 * Michael
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688123)
 * You beat to it. Please refer to my previous reply.
 *  Thread Starter [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688168)
 * Wow!
 * Thank you!
 * This is really great!
 * I am sorry if my “newbie” is showing, but I wondered if you might be able to 
   point me to a place where I might be able to learn about adding the zero-padded
   number to the field set?
 * I am using the Custom Field Template plugin to generate the fields on a custom
   content type.
 * I really do appreciate all of your help!
 * Thank you so much!
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688182)
 * I have never used the Custom Field Template plugin, so I don’t know how it works.
 * Do you type in the values for the custom fields? If so, just type in the value
   of the key as well.
 *  Thread Starter [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688188)
 * Oh, the plugin just creates a UI for adding custom fields, well that is not ALL
   it does, but I cant claim to fully understand its other features.
 * Hahahah!
 * I have to imagine that there is a way to “count” and assign a unique ID to each
   instance of each custom field, and have it relatively “transparent” on the content
   type edit screen.
 * I just have not yet wrapped my head around it yet.
 * Thank you so much for all your help.
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688192)
 * I can’t imagine that it would be very simple to do what you are wanting. What
   happens if you only fill in one value of a pair? What happens if you go back 
   tomorrow and edit one of a pair?
 * If the plugin adds the key, it will have to show up in the value box. The user
   could then edit it and break the link.
 * This could get nasty.
 *  Thread Starter [Michael](https://wordpress.org/support/users/mlove2k2/)
 * (@mlove2k2)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688216)
 * Yes, I am beginning to think you are very right about that!
 * What I really want to do is create a weekly calendar that is connected to a custom
   post time so that the calendar of events is not updated in “real” time, but rather
   each week when that post is published.
 * I guess I should probably look in another direction. Perhaps an already existing
   plugin that will allow for this type of thing.
 * Its less of a calendar and more of a list of weekly events.
 * In theory it does not sound too hard, but I am clearly doing something off-base!
 * Hahahaha!
 * Thank you so much for your help!
 * Michael
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688226)
 * I may be able to help if you are using a free theme that I can download to my
   test site. I will need a lot more detail, though.
    - What do you want to see on the screen?
    - Who enters the data?
    - How often is it entered?
    - How will you get to the screen? Does it need to show a post, or can it be 
      a static page?
 * Email me at mac =at= mcdspot =dot= com with as much detail as you can.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Custom Field Array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [display](https://wordpress.org/support/topic-tag/display/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/custom-field-array/#post-1688226)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
