Wordpress删除查询不起作用

时间:2014-09-18 16:44:30

标签: wordpress-plugin wordpress

我正在尝试从表中删除照片。当我尝试在同一个文件上删除它时显示我"致命错误:调用未定义的函数register_activation_hook()"信息。还尝试了另一个文件,但无法修复它。

  <?php
function delete(){
global $wpdb;
$table_name = $wpdb->prefix . "test";
$id=$_GET['id'];
if($id){
$delete = $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->$table_name WHERE id = %d",$id));
 }  
 } ?>

2 个答案:

答案 0 :(得分:0)

$ table_name正在获取其值,因此无需编写&#34; $ wpdb-&gt; $ table_name&#34;

<?php
  function delete(){
    global $wpdb;
    $table_name = $wpdb->prefix . "test";
    $id=$_GET['id'];
      if($id){
         $delete = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE id = %d",$id));
       }  
} ?>

删除照片的另一种方法是使用wp_delete_post,因为在wordpress照片中,媒体会保存为帖子。

<?php wp_delete_post( $postid, true); ?>

第二个参数设置为强制删除图像

答案 1 :(得分:-2)

试一试。我之前在我的一个插件中使用过它

http://codex.wordpress.org/Class_Reference/wpdb#DELETE_Rows

$wpdb->delete( $table, $where, $where_format = null );

举个例子:

<?php
    global $wpdb;
    $table = $wpdb->prefix . 'my_table';
    $where = array( 'event_id' => $event_id);  // $where part
    $where_format = array( '%d'); // $where_format
    $wpdb->delete( $table, $where, $where_format );
?>
相关问题