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?
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?
Share Improve this question edited Jan 16, 2021 at 18:31 photogenic asked Jan 16, 2021 at 15:58 photogenicphotogenic 211 silver badge7 bronze badges 1 |1 Answer
Reset to default 0How 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.
10
would match100
). 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 ♦ Commented Jan 16, 2021 at 19:38