• Resolved Cas Dekkers

    (@casdekkers)


    Hi,

    I’m in the process of creating my own custom post type plugin, and I’m currently pondering the best way to save my own post type’s metadata to the database.

    On one hand, it’s benificial to divide all meta data over multiple keys. For example, when integrating an event post type with a start date and an end date attribute, you could choose to save both of these separately by invoking yhe update_post_meta() method twice for two different meta keys. It costs a bit of database space, but it’s easy to query later on (e.g. https://codex.www.ads-software.com/Class_Reference/WP_Meta_Query).

    On the other hand, you could choose to save these date attributes in the database as an array, thus using only one key. But then, you can’t really use the WP_Meta_Query() easily in a later stage.

    Which would be best when you have, say, 20 attributes: storing them all 20 separately, or splitting them among (less than 20) meta keys as arrays?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Are all the attributes ones that would be potential query variables? Or only some? Any that would not be potential query vars could all be stored in a single array, and the query var candidates stored separately.

    It’s also possible to further filter results in PHP after the query for those items in the array. It’s certainly less efficient computationally, but arrays do keep the DB more compact should there be a huge number of posts with a lot of meta data. Filtering in PHP also means you’d have to manage your own pagination, which gets tricky since you don’t know how many posts you will need to fill a page.

    If DB efficiency due to a large number of posts is an issue, it’s worth considering a custom table to contain your meta data. The fields can be tailored more efficiently to the type of data, where the post meta table uses varchar for everything, even a boolean value.

    Thread Starter Cas Dekkers

    (@casdekkers)

    I’ll mark this as resolved. Thank you for your answer, bcworkz! I will consider combining both approaches.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Querying a meta_value of type array’ is closed to new replies.