Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building blog where users ll have option to download images. I want to display information how many times post have been displayed and how many times the image have been downloaded.

Technically I have solved this by using custom fields and it works. In near future I want to add option to sort images based on vies/downloads. Also I have solved this, but what Im thinking now is that maybe I should use custom tables instead of custom fields.

I plan to have about thousand of images, and I'm worrying if queries for displaying most downloaded/viewed images will be slow.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use a custom table for a view counter. This is faster, easier to write, and it has fewer side-effects.

Post meta queries are rather slow. The information is stored in a way optimized for average post handling, not for custom sorting or many write accesses. Also, all post meta values are fetched each time someone asks for any field. Your data will use resources even when they aren’t needed.

Sooner or later you might want to count archive views too, multiple images on one page, or downloads of archive files with more than one file. Where do you want to store that? You have much more freedom with separate tables: they are easier to extend, you can add or remove fields, tables or indexes.

Other plugins might do unexpected things with your data, so you cannot rely on it anymore.

An example: In Multilingual Press, when someone creates a translation of an existing post, I take all post meta fields (except some known built-ins) and copy those over to the other blog. I have to, because otherwise many plugins would break. But your plugin would get a wrong counter now: The new post has an incorrect view count before anyone has ever seen it.
You can protect your fields in Multilingual Press, but in other plugins, that might not be possible, or you just don’t know they exist.

I have also written a small blog post about this decision with more arguments: How to store my custom values?

See also this answer: Post meta vs Seperate database tables

share|improve this answer
    
Thanks for answering! What do you think of using core tables (in this case _posts)? I think that in my case it would be good option, since i ll need to add only two more columns. –  RhymeGuy Sep 26 '13 at 7:15
    
No, do not change existing tables, this is not safe. –  toscho Sep 26 '13 at 9:02
    
OK :), thanks again! –  RhymeGuy Sep 26 '13 at 11:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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