最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - How to improve my non-unique metadata MySQL entries?

programmeradmin0浏览0评论

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
  • 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 Commented Jan 16, 2021 at 19:38
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论