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.
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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
