I have many posts with a unique reference for a custom field [123456], over time I would like an administrator to upload a single image that matches the custom field ie > 123456.jpg

I would then like to use a condition in the loop that says if file exists (123456.jpg) that matches the custom field (123456) then use that image for all posts with that custom field...

I cant use the Media library because the folder structure is chronological, is there another way?

1 Answer 1


You can still use the media library and either reference the attachment's ID or add a postmeta to the attachment containing your id.

You could even make this a bit easier by using a custom taxonomy instead of custom field. Add your numbers as terms to that taxonomy. Also register the taxonomy to apply to attachments. Then write a bit of code that whenever the post has (a) term(s) in that taxonomy finds the attachments that also have a the same term(s) in that taxonomy. This would even allow you to have both, multiple of your numbers for a post and multiple images per number, leading to all kinds of possible combinations.

  • 1
    wow that made my head spin, don't understand that without seeing the code
    – erichmond
    Feb 24, 2011 at 23:06
  • Sorry, too busy to write it for you. But i'll try to make it a bit clearer...just imagine your custom taxonomy would be the tags. So you add tags capability to attachments (google for `wordpress register taxonomy for object'). Now instead of adding your 'special number' as a custom field, you add it as a tag. Admin then adds the same number as a tag to attachments. The only custom code you need then goes something like this: - fetch tags for post, - for each tag, fetch the attachment(s) with the same tag, - for each found attachment, get the file url. WP has functions for all of that.
    – wyrfel
    Feb 25, 2011 at 1:03
  • Yes that makes perfect sence, using The taxonomy rather than a custom field, having given it more thought I'm not actually sure I've thought of the best solution. 20 post might need to use the same image but I want the user to upload 1 image and the posts to use it if it exists without the user having to go into each post and add the taxonomy.
    – erichmond
    Feb 26, 2011 at 8:39
  • @erichmond Well, if you add the taxonomy or a custom field is the same in the end, isn't it. You'll have to write some code to do it automatically in either case.
    – wyrfel
    Feb 26, 2011 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.