WordPress Growth Stack Alternate is a query and reply web site for WordPress builders and directors. It solely takes a minute to enroll.
Anyone can ask a query
Anyone can reply
The most effective solutions are voted up and rise to the highest
Requested
Considered
8 instances
I’ve been looking for out whether it is higher to retailer customized meta field fields for a customized publish sort individually or as a serialised array. On the internet I’ve seen totally different approaches however which one is extra ‘right’? The argument for the serialised array is that it solely fills ‘one area’ within the database versus a number of rows but it surely seems like it’s troublesome to carry out WP_QUERY
on a serialised array and troublesome to carry out searches.
I’m a bit apprehensive now about which route I ought to go as I do not wish to decide one solely to search out months down the road when I’ve a great deal of information that I made a mistake.
Mainly this:
a:4:{s:4:"identify";s:5:"Bobby";s:5:"e-mail";s:15:"bobby@gmail.com";}
vs two rows (I’ll have extra, however that is simply an instance)
identify - Bobby
e-mail - bobby@gmail.com
serialised PHP has safety dangers and may trigger issues throughout search replaces should you change the size of values
The argument for the serialised array is that it solely fills ‘one area’ within the database versus a number of rows but it surely seems like it’s troublesome to carry out WP_QUERY on a serialised array and troublesome to carry out searches.
Sure, you possibly can’t reliably question sub-sections of serialized information, and a few sorts of question aren’t doable in any respect. Should you should serialize, do it with JSON, however that solely mitigates some minor points.
Have in mind:
- publish meta/customized fields are quick if you already know the publish ID
get_post_meta
could be very quick, WP will prefetch the publish meta so calling it twice doesn’t create duplicate queriesmeta_query
/looking for and filtering by publish meta is sluggish, and doesn’t scale. If you might want to filter, seek for posts, then retailer that particular piece of knowledge in a customized taxonomy.- For instance, a
product_category
publish meta, or asort
publish meta is a horrible concept, however they’re all nice as taxonomies
- For instance, a
- You possibly can have a couple of meta worth with the identical key, meta keys usually are not distinctive, e.g.
$vehicles = get_post_meta( $post_id, 'automobile', false ); foreach ( $vehicles as $automobile ) {...
- WP serializes arrays and objects if you attempt to save them to be useful, however WP is caught doing this for backwards compatibility causes and can’t cease. This doesn’t imply it is a good factor to do
- You will see that many individuals who’ve spent their whole careers doing issues fully with publish meta who will resist recommendation in any other case, or assume you are advocating a complete substitute. This stuff all have tradeoffs and are nuanced, the right reply for the best way to retailer your information goes to depend upon how you employ your information
- if that you’re solely going to show this information when on the publish/web page in a template, and can by no means want to look/filter for it, then you definitely’re effective to retailer a JSON serialized construction. Simply perceive that to the database, a publish meta worth is only a string of textual content. I am unable to see sub-structure, and people tables usually are not optimised to look or filter for it.
- this additionally means you possibly can’t replace/delete change sub-sections both, it’s a must to fetch the complete construction, modify it in PHP, then save the complete worth again. Identical with deletion and addition and so forth
default