在Wordpress中运行查询

时间:2016-05-05 10:34:40

标签: php wordpress

每次我的客户端上传产品时都需要运行此查询,那么如何在Wordpress中运行此查询?我想把它绑定到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')

1 个答案:

答案 0 :(得分:0)

在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 );