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 could use some advise on how to structure my data for performance. I have a custom post type with a lot of custom meta data stored in the wp_postmeta table as a serialized array under one meta key for each post. Now, I need to get some posts from that post type, and filter them by the meta data I have stored.

Right now, the only way I can think to do that is to get ALL the meta data for ALL of the posts of this type, then unserialize them into a massive PHP array, and then use foreach loops to check them all one-by-one. This strikes me as a really bad/slow way to do things.

At this point, I'm thinking I should scrap my whole plan, and start from scratch using my own custom table. That's just so un-wordpress though, and it would be a LOT of work. My other idea is to do what was suggested on this other question (meta_query with meta values as serialize arrays) and separate all the meta values into separate meta keys and then query them as shown on that question. The problem I see there is that that query doesn't look like it will be particularly fast either, and my wp_postmeta table would get HUGE.

Any ideas would be greatly appreciated.

share|improve this question
    
Storing the meta values into separate custom fields in wp_postmeta seems the cleanest way to me. Is this really giving you a performance problem, or do you think it will? Database lookups are fast and can be cached. –  Geert Dec 12 '11 at 8:47
    
@Geert, thanks for suggestion, that's probably what I'll have to do. To answer your question, the site is still in development, so I'm just thinking it will be slow. –  Dominic P Dec 12 '11 at 11:29
add comment

1 Answer 1

up vote 4 down vote accepted

If you want to have more control over the filtering of your posts then separating the meta values into separate keys is the best option to go about it. Also what you could do is, only the values that would be required while filtering could be separated and the rest of them could go into a serialized array.

share|improve this answer
    
thanks, I hadn't thought of only breaking out the one's I'm going to filter by. And, you don't think that query will be too slow? I would probably be filtering by 5-6 different meta fields. –  Dominic P Dec 12 '11 at 11:34
    
Nope it wouldn't affect the page load that much. –  Joshua Abenazer Dec 12 '11 at 11:41
    
ok, this seems like the best plan. I just see one more potential trouble spot. After I filter the posts using a meta_query, I will still need to display the meta data with the post. So, as far as I can tell, I'm right back to looping through the results from WP_Query and calling get_post_meta for each one. This seems like an inefficient way to do it. Is there a better way you know of? –  Dominic P Dec 12 '11 at 20:56
    
Just for anyone else that's interested. I decided to store all of the relevant meta data in a serialized array in the post_content field as well as in the wp_postmeta table. This way, when I get the post data with WP_Query, I can just loop through it and unserialize all the meta data into one big array without any additional queries. Thanks for all the help. –  Dominic P Dec 22 '11 at 6:12
add comment

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.