问题描述:

I need to run this query every time my client uploads a product, so how do I run this query within Wordpress? I want to tie it to an action in the functions.php

UPDATE wp_postmeta

JOIN wp_posts ON wp_posts.id = wp_postmeta.post_id

SET wp_postmeta.meta_value = 'no'

WHERE (wp_posts.post_title = '4x6' AND wp_postmeta.meta_key = '_virtual')

网友答案:

Add this code in your functions.php:

function on_product_publish( $new_status, $old_status, $post ) {

    global $post;

    if ( 'publish' !== $new_status or 'publish' === $old_status ) return;

    $wpdb->query("UPDATE wp_postmeta
    JOIN wp_posts ON wp_posts.id = wp_postmeta.post_id
    SET wp_postmeta.meta_value = 'no'
    WHERE (wp_posts.post_title = '4x6' AND wp_postmeta.meta_key = '_virtual')");

}

add_action( 'transition_post_status', 'on_product_publish', 10, 3 );
相关阅读:
Top