Associate custom fields together
I want to find a way to associate 2 custom fields. For example: We have setup a Custom Post Type for products. Each product can have X number of videos associated with it. For each video, we need the URL (on YouTube) and a description. So really we need someway to add Custom Fields where we can enter two items: URL / Description.
Is something like this possible?
Yes, use a character as a delimiter and parse the custom field into an array using PHP
youtube.com?V=2372372983|Great music video
I’ve placed a pipe character | between the two parts
The trick is to use a separator that will NEVER appear in the actual URL or description.
In searching turns out I answered this last year – you are interested in the “explode” part of the code you will see in this article
The line is
$temp = explode("&", "$product");
If you use a pipe as the separator your code would be
$temp = explode("|", "$product");
rather than an ampersand
You have to look at the full code in that post to see how to use this.
Explaining separators to clients is not what I want to do unfortunately – no plugins or other options?
I just realized I answered your question too quickly not taking everything you said into consideration. I’m sorry.
Linking a URL to a Description is easy. It gets trickier when you need multiple occurrences of that pair per post and you don’t know how many there are.
The way to link a URL to a description is have two custom fields, URL and Description, and have your code call for them. That is easy.
Whether the URL/Descrip is in one custom field or two is not the real issue. The real issue, since your client is entering these is, even if you could have two data fields in one custom field, what would you name the custom fields? You can only have one custom field with the same name on a single post.
You’d have to name them something like
and a matching
Since these are being manually entered the likelihood of user error in the naming process is high. Your code can process all the custom fields associated with a post and match up the URL & Descrip, but you have to match them by the sequence # also.
It is possible there is a plugin to handle this. But I don’t really see how that would be written. What you are looking at really is a custom data entry screen that writes the custom fields into the database assigning its own sequence # so no user can screw it up by entering custom field names manually incorrectly. You can’t count on that being done accurately.
If there are hundreds or thousands of products it could even make sense to use a custom database table. If its a smaller project you could do it in the same tables the custom fields use. You would be using a mechanism other than the custom field screen to get the data in and out.
The issue you have is its not a simple process to do what you are asking for. You could also do it by having a custom post type for the URL/Descrip data and link that to the original product post using the product post’s ID #. that would require up to 9 posts per product. (That method mirrors the way things actually get stored in the database.)
- The topic ‘Associate custom fields together’ is closed to new replies.