Multiple Values Wordpress Custom Fields

So i had a client who needed to enter into their site a list of the manufactures that they sell in their store. In the past, i have set up a category called "Manufacturers" and then told them to create a new post for each one. The following client, just wanted the manufacturer name, logo, and website. So i created 1 wordpress page called "manufacturers" and created a custom field on that page called "manufacturer info." What is cool is that you can have more than one custom field with the same name on a page and wordpress will automatically create an array of them. I then bulk added the logos to the media library (after making them all the same size). So they were all pathed to /wp-content/uploads/file_name.jpg So i then added the info to the custom field in the following manner into the "value" field. Company Name, http://companyurl.com, company_logo.jpg and added about 50 manufacturers. So the client wanted 12 random manufacturers to be shown on their homepage in a certain section every time the page reloaded to keep it fresh. I added the following code to the hompage

// Enter in the page id of your page that holds the custom fields
<? query_posts('page_id=155'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                                                        
<?

// create a variable that stores the custom fields
$meta = get_post_custom_values("manufacturer info");

// Use "shuffle" to create a new order each time the page loads
$metas = shuffle($meta);
$count = 0;

// do a foreach loop to get the manufacturers
foreach ($meta as $m) {

// this is the work here...this takes each manufacturer and creates an array of their values.  In this case $info = manufacturer name, $info[1] = manufacturer url, and $info[2] = manufacturer logo    
$info = explode(", ", $m);
echo "<div>";
echo "<a href="" . $info[1] . "">"; 
echo "<img src="/wp-content/uploads/" . $info[2] . "" width="100">";
echo "</a>";
echo "</div>";
$count++;
if ($count == 12) break; // This stops printing the array to the screen at 12 logos.  So each time the page loads, 12 new logos will be loaded
}               

?>

<? endwhile; else: endif; wp_reset_query();?>

Post a comment
  1. Jermaine

    You can also have the Company Name, Logo and website on separate lines within the same custom field box: Learning how to split and categorise values within the same custom field is a smart trick I discovered: http://www.graphicbeacon.com/how-to-split-and-categorize-wordpress-custom-field-values/

    • Matt Price

      Using a line break is a good idea. anything that you can "explode" with will work on the custom fields. Thanks Matt

  2. Matt Price

    Hi Thomas My blog post is not for what you are trying to do. Mine is for if you have one item that has the same meta_key but many different meta_values. You can have the same meta_key and enter in new values...so for example customfield = ibm customfield = apple customfield = dell when you "get_post_meta" for customfield...wordpress automatically makes an array of the values. I used this so that i could have one page with all my manufacturers on it and then i could also "do stuff" with the array of manufacturers. For example on the homepage i want to show 5 out of the 50 manufacturers in a random way. My blog article explains how to do that I can look into solving your issue in the next week or so. Also...when you sort dates make sure to use YYYY/MM/DD. If you were to use MM/DD/YYYY all your course dates would be associated by month from year to year will be next to each other and not separated by year. eg... 07/08/1999 08/10/2011 08/24/2010 as opposed to 1999/07/08 2010/08/24 2011/08/10 Matt

  3. Tomáš Kapler

    Yes, this is easy but the problem is, that it is not possible to search/filter it. Imagine you want to get only posts where at least one of the custom field is some special manufacturer. Or another example: posts=courses. And you need to add custom fields: dates and places. So e.g. 1.1.2010 - London 12.5.2010 - New York I could save it all in one custom field imploded by e.g. coma. But then imagine you want to get only London courses. Or only courses in next 1 week (in any city). It would be a big problem. I'm now wondering about good solution of such problem and the only what i have found is 2 custom fields: dates & places. So in dates array i would have {1.1.2010;12.5.2010} and in places {London;New York} and pair them so that the position in the array would define a pair. Than i can easily use wordpress function to filter by date or by place. The problem afaik is filltering by date AND by place (London courses in next week) and i do not know how to solve this.






^