adding post meta with sql query
-
I have been trying to arrange my site so that my URL structure is set to domain.com/collection/%collection%/%product-group%
Where %collection% is a taxonomy term, collection is the index, and %product-group% is the product in question (its a group cause each group will list many product variations)
Anyway, wordpress seems unable to handle this simple task, so I am switching methods in order to get my permalink structure correct.
I am simply going to create the collection, product, and variant pages all under the same custom post type with no taxonomies to deal with. Then each will display a page template depending on the value of a custom field (collection, product, or variant)
Well, since I already have the products and variants already uploaded to their respective custom post types, I don’t want to go through all the data again to set featured images and content. (If it werent for that, I would just re import all the data.)
So I created a function to switch post types in the data base for me. It works except for one part.
here is the code
function change_post_type($from,$to,$value) { global $wpdb; //change metadata $post_id = $wpdb->get_results ('SELECT ID FROM wp_hyhg_posts WHERE post_type=pods_product_grps' , object); foreach ($post_id as $id){ $table='wp_hyhg_postmeta'; $data= array( 'post_id' => $id, 'meta_key' =>'input_type', 'meta_value' => $value ); $wpdb->insert( $table, $data ); } //UPDATE $data2= array( 'post_type' => $to, ); $where =array('post_type' => $from ); $wpdb->update('wp_hyhg_posts', $data2, $where); } change_post_type ('pods_product_grps', 'uc-product', 'product');
So, I was able to get the post type to switch from ‘pods_product_grps to uc-product easily enough, but I also need to add a meta value ‘input_type’ to every product I just switched.
Since it is already changed, I can simply add input_type=>product to all products currently labeled as uc-product
But I also need to run this code where the function is:
change_post_type (‘product_variant’, ‘uc-product’, ‘variant’);
So I need to get the meta data figured out for the ones I just converted, before I add the new set of data to the mix.
The product and Variants input type both have additional sets of meta data that are displayed conditionally based on the value selected.
Products will have a field called group_id and variants will have a field (product sku) which contains the group_id plus a number
So I need another query that finds all post marked ‘product’, gets the post_id, then compares the group_id to product_sku and updates the parent_id field to the corresponding parent relationship.
That is probably confusing. In other words, a product variant is a child of a product if group_id is in product_sku So I’d like to make a query that populates the parent field of the post table based on that condition.
Hope someone can help with this one.
- The topic ‘adding post meta with sql query’ is closed to new replies.