如果产品具有元价值,则将产品列表更新为特色(MySQL不是wpQuery)

时间:2020-01-26 作者:ilanb

因为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!
        }
    }
}
尚未测试。

1 个回复
SO网友:ilanb

Ok I\'v创建了一个管理插件来执行此操作。。。如果你有更好的主意:-)我加入了

创建“管理”按钮以打开meta\\u值的csv文件,与现有产品进行比较。并将产品更新为特色产品(如果存在)。

add_action(\'admin_menu\', \'featured_button_menu\');
function featured_button_menu()
{
    add_menu_page(\'Featured Products Page\', \'Mise en avant produits\', \'manage_options\', \'featured-button-slug\', \'featured_button_admin_page\');
}

function featured_button_admin_page()
{
    // General check for user permissions.
    if (!current_user_can(\'manage_options\'))
    {
        wp_die( __(\'You do not have sufficient pilchards to access this page.\'));
    }

    // Start building the page
    echo \'<div class="wrap">\';
    echo \'<h2>Mettre à jour les produits mise en avant (CSV)</h2>\';

    // Check whether the button has been pressed AND also check the nonce
    if (isset($_POST[\'featured_button\']) && check_admin_referer(\'featured_button_clicked\'))
    {
        // the button has been pressed AND we\'ve passed the security check
        featured_button_action();
    }
    echo \'<form action="options-general.php?page=featured-button-slug" method="post">\';
    // WordPress security feature
    wp_nonce_field(\'featured_button_clicked\');
    echo \'<input type="hidden" value="true" name="featured_button" />\';
    submit_button(\'Lancer la procédure\');
    echo \'</form>\';
    echo \'</div>\';
}

function featured_button_action()
{
    global $wpdb;

    $filepath = $_SERVER[\'DOCUMENT_ROOT\']."/data/featured.csv";

    $cip_array = [];
    $resInt = [];

    $handle = fopen($filepath, "r") or die("Error opening file");
    while(($line = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $cip_array[] = $line;
    }
    fclose($handle);

    foreach($cip_array as $res) {
        $resUTF8 = mb_convert_encoding($res[0], \'UTF-8\');
        $resInt[] = intval($resUTF8);
    }
    $List = implode(\',\', $resInt);

    $products = $wpdb->get_results("SELECT phiz_posts.ID
                            FROM phiz_posts
                                JOIN phiz_postmeta ON ( phiz_posts.ID = phiz_postmeta.post_id )
                                JOIN phiz_term_relationships ON ( phiz_posts.ID = phiz_term_relationships.object_id )
                                JOIN phiz_term_taxonomy ON ( phiz_term_relationships.term_taxonomy_id = phiz_term_taxonomy.term_taxonomy_id )
                                JOIN phiz_terms ON ( phiz_term_taxonomy.term_id = phiz_terms.term_id )
                            WHERE phiz_posts.post_status =  \'publish\'
                                AND phiz_posts.post_type =  \'product\'
                                AND phiz_postmeta.meta_key =  \'product_cip\'
                                AND phiz_postmeta.meta_value IN (.$List.) ");

    foreach ($products as $prd)
    {
        $term_taxonomy_ids = wp_set_object_terms( $prd->ID, \'featured\', \'product_visibility\' );
        if ( is_wp_error( $term_taxonomy_ids ) ) {
            echo "There was an error somewhere and the terms couldn\'t be set.<br>";
        }
    }
}

相关推荐

MySQL使用‘UPDATE Text’更新文本字段

我需要有一个charvar字段,其中包含Woody的撇号。我正在使用wp\\u list\\u table来简化表的编辑。更新后,该字段包含Woody/\'s。我可以通过使用Woody\'s使用phpMyAdmin更新数据。我认为在数据发送到wp\\U list\\u表之前,我可以将其更改为Woody\'s,但最终在数据库中显示为Woody/\'/\'s。我如何实现这一点?