因为woo 3的特色产品是分类法“product\\u可见性”的名称和鼻涕虫。
我需要将列表中包含的具有相同meta\\u值的所有产品更新为“特色产品”。
我的产品有一个meta\\u密钥=\'product\\u cip\'
我要与“product\\u cip”比较的cip列表有(123465712637094563832837472)等。
我可以选择像这样的所有特色产品,但不知道如何更新:
SELECT post_title, ID FROM wp_posts
INNER JOIN wp_postmeta wm ON (wm.post_id = wp_posts.ID)
INNER JOIN wp_term_relationships wtr ON (wp_posts.ID = wtr.object_id)
INNER JOIN wp_term_taxonomy wtt ON (wtr.term_taxonomy_id = wtt.term_taxonomy_id)
INNER JOIN phiz_terms wt ON (wt.term_id = wtt.term_id) AND ((wtt.taxonomy = \'product_visibility\' AND wt.slug = \'featured\'))
WHERE post_type = \'product\' AND post_status = \'publish\'
谢谢你,
编辑:
global $wpdb;
// Select all products needed to be updated as \'featured\'
$q = \'SELECT wp_posts.ID \';
$q .= \'FROM wp_posts \';
// Attach wp_postmeta table
$q .= \'JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) \';
// Attach taxonomy and term tables
$q .= \'JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id ) \';
$q .= \'JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) \';
$q .= \'JOIN wp_terms ON ( wp_term_taxonomy.term_id = wp_terms.term_id ) \';
// WHERE statements
$q .= \'WHERE \';
// Post should be published
$q .= \'wp_posts.post_status = "publish" \';
$q .= \'AND \';
// Post should have either product as post_type
$q .= \'wp_posts.post_type = "product" \';
$q .= \'AND \';
// Post should have meta_key = product_cip
$q .= \'(\';
$q .= \'wp_postmeta.meta_key = "product_cip" \';
$q .= \'AND \';
$q .= \'wp_postmeta.meta_value IN ("3664592000014","3401097399423","4015630064779","3400941662003","3400941631245","3401321104311","3401051049166","3400936760578","3400938341836","3401021104543","3400939035765","4015630057009","3401097592640","3401060246723","3662042003295","3401096745795","3401097024363","3401099724896","3400935709042","3400935167156","3401021104482","3400936751934","3401096745856","3400936348288","3400934965852","3400922096612","3400933043445","3664490000031","3400932897162","7323190196562","3400936666795","3400939472898") \';
$q .= \')\';
$wpdb->get_results($q);
有了这个,我得到了需要更新的产品,如何将这个结果更改为特色产品?
编辑2:
function update_products_tofeatured_for_home($atts)
{
global $wpdb;
// Select all products needed to be updated as \'featured\'
$q = \'SELECT wp_posts.ID \';
$q .= \'FROM wp_posts \';
// Attach wp_postmeta table
$q .= \'JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) \';
// Attach taxonomy and term tables
$q .= \'JOIN wp_term_relationships ON ( wp_posts.ID = wp_term_relationships.object_id ) \';
$q .= \'JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ) \';
$q .= \'JOIN wp_terms ON ( wp_term_taxonomy.term_id = wp_terms.term_id ) \';
// WHERE statements
$q .= \'WHERE \';
// Post should be published
$q .= \'wp_posts.post_status = "publish" \';
$q .= \'AND \';
// Post should have either product as post_type
$q .= \'wp_posts.post_type = "product" \';
$q .= \'AND \';
// Post should have meta_key = product_cip
$q .= \'(\';
$q .= \'wp_postmeta.meta_key = "product_cip" \';
$q .= \'AND \';
$q .= \'wp_postmeta.meta_value IN ("3664592000014","3401097399423","4015630064779","3400941662003","3400941631245","3401321104311","3401051049166","3400936760578","3400938341836","3401021104543","3400939035765","4015630057009","3401097592640","3401060246723","3662042003295","3401096745795","3401097024363","3401099724896","3400935709042","3400935167156","3401021104482","3400936751934","3401096745856","3400936348288","3400934965852","3400922096612","3400933043445","3664490000031","3400932897162","7323190196562","3400936666795","3400939472898") \';
$q .= \')\';
$products = $wpdb->get_results($q);
//var_dump($products);
foreach ($products as $prd)
{
$term_taxonomy_ids = wp_set_object_terms( $prd, \'featured\', \'product_visibility\' );
if ( is_wp_error( $term_taxonomy_ids ) ) {
// There was an error somewhere and the terms couldn\'t be set.
} else {
// Success!
}
}
}
尚未测试。