Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top