I'm working on a plugin for WooCommerce which uses a cpt called Restock. In a Restock post you can enter products ( id ) plus restock. The user can add as many product restock pairs as they want per post.

An example post

I'm currently saving each id/restock-entry as an associative array inside the Metadata value. The last parameter ($unique) inside add_post_meta is set to false, so I can add as many values as products have been restocked.

foreach ( $new_content as $new_product ) {
    $new_product = array ( id => 1313, restock => 55 );
    add_post_meta( $post_id, 'rs_products', $new_product, false ); 

I believe this is not the most optimale way how to save the metadata and I would like to improve this before I start connecting the data to WC.

How would you save such a repetitive data pair inside MySQL?

  • I actually disagree, lots of people use far worse methods such as comma separated lists, then run into problems, e.g. you can't query for IDs in a comma separated list without false positives ( e.g. a search for 10 would match 100 ). If you're going to be filtering and searching for posts with specific post meta values though then that's another story entirely
    – Tom J Nowell
    Jan 16, 2021 at 19:38

1 Answer 1


How would you save such a repetitive data pair inside MySQL?

As separate key/value pairs, which is what you have already done.

The only situation I would reconsider this, is if you need to filter or search for restock posts via these IDs. If that is the case then I would use a private/hidden taxonomy where each term has the post ID as the slug. Post meta is not good for searches/filtering.

  • Using taxonomies is an interesting option, I will look into that. About the Metadata: I will definitely filter/search product ids and their respective restocks inside the restock posts. I was asking about better ways to save the data, because I know that arrays saved in MySQL can't be queried. So I would need to constantly serialize the data or save it as a json. Which doesn't seem optimale to me...
    – photogenic
    Jan 17, 2021 at 9:09
  • serializing the data/using JSON would mean that searching/filtering would be impossible, don't store serialised structured data in the database, it's a major anti-pattern. And when I say search/filter, I mean any kind of query that restricts which posts are fetched based on post meta. E.g. find all restock posts that mention this product ID in their post meta. Those queries will get ultra expensive/slow/heavy.
    – Tom J Nowell
    Jan 17, 2021 at 10:48
  • I understand. Then I will follow your suggestion and create a taxonomy called "restocked products". Only thing I'm asking myself now, is how I can query the stock of a specific product inside a restock post?
    – photogenic
    Jan 17, 2021 at 12:04
  • I don't know, you'd have to ask that in a WooCommerce community
    – Tom J Nowell
    Jan 17, 2021 at 12:09
  • Sorry, I didn't phrase this correctly. I don't mean the stock of the WC product but restock stock which I added inside the restock post. Lets say I have added an id and stock to a restock post. The id is add to the taxonomy. ID and post are easily queried now. But how should I query the stock which is currently saved inside the meta as an array? First thought is to add a metakey restock_stock_{id} with the stock as a value to the restock post?
    – photogenic
    Jan 17, 2021 at 13:38

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.