dty98339 2015-05-05 06:39
浏览 14
已采纳

如何在wordpress中使用更新和删除查询

First i write manually update, delete, insert and select query and execute data with mysql_query function

Like this:

Select query

$prefix = $wpdb->prefix;
$postSql = "SELECT DISTINCT post_id
            FROM " . $prefix . "postmeta As meta
            Inner Join " . $prefix . "posts As post
            On post.ID = meta.post_id
            Where post_type = 'product' 
            And post_status = 'publish'
            And meta_key Like '%product_img%'";
$postQry = mysql_query($postSql);
while ($postRow = mysql_fetch_array($postQry)) {
     $post_id = $postRow['post_id'];
}

Insert Query

$insert_images = "Insert Into " . $prefix . "postmeta(post_id,meta_key,meta_value) Value('$post_id','$meta_key','$data_serialize')";
        mysql_query($insert_images);

Update Query:

$update_price = "Update " . $prefix . "postmeta
                         Set meta_key = 'wpc_product_price'
                         Where post_id = $supportMetaID
                         And meta_key Like '%product_price%'";
 mysql_query($update_price);

Delete Query

mysql_query("Delete From " . $prefix . "postmeta Where meta_key IN ('product_img1','product_img2','product_img3')");

All queries are working perfectly ... but now i want to embed all queries in wordpress queries.

I can also use wordpress queries like

$wpdb->get_results( "SELECT id, name FROM mytable" );
$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
);
$wpdb->update( 
    'table', 
    array( 
        'column1' => 'value1',  // string
        'column2' => 'value2'   // integer (number) 
    ), 
    array( 'ID' => 1 )
);
$wpdb->delete( 'table', array( 'ID' => 1 ) );

But you can see that i use and / or conditions in my queries. So any body help me how can i embed my queries in wordpress

  • 写回答

2条回答 默认 最新

  • doushen1026 2015-05-05 06:46
    关注

    Use $wpdb->prepare for the queries involving different criteria

    $wpdb->query( 
        $wpdb->prepare( 
            "Update " . $prefix . "postmeta
             Set meta_key = %s
             Where post_id = %d
             And meta_key Like '%product_price%'" ,
             'wpc_product_price',$supportMetaID 
            )
    );
    

    Also if your are inserting/deleting from postmeta is suggest you to use WP's builtin functions update_post_meta/delete_post_meta

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)