【问题标题】:DELETE FROM wp_posts WHERE post_author = $userID;从 wp_posts 中删除 post_author = $userID;
【发布时间】:2013-03-27 17:04:43
【问题描述】:

我的functions.php中有这段代码

global $current_user;
$userid = $current_user->ID;
$args = array(
    'post_type' => 'listings',
    'post_status' => 'publish',
    'author' => $userid
);
$the_posts = get_posts ( $args ); // get the published posts for that author
$post_count = count($the_posts); // count the number of published posts for the author
$N = 2; // set number for max posts per user
if ($post_count > $N) {
    if (current_user_is('s2member_level1')) {   


       // This is where I want to delete from wp_post where post_author = $userID


    }
}

我对 SQL 查询不够熟悉,无法弄清楚它为什么不起作用。我试过了

$wpdb = "DELETE FROM wp_posts WHERE post_author = $userID;"

$wpdb->query("DELETE FROM wp_posts WHERE post_author = $userID;");

全局 $wpdb;在我的 php 文件中早先定义

【问题讨论】:

标签: php mysql sql wordpress


【解决方案1】:

Delete 应该不带 * 字符。

【讨论】:

    【解决方案2】:

    查询应该是这样的:

    DELETE FROM table WHERE ....;
    

    在你的情况下:

    "DELETE FROM wp_posts WHERE post_author = $userID;"
    

    【讨论】:

    【解决方案3】:

    尝试使用 $user_ID = get_current_user_id();在全局 $current_user; 之后。或声明 $current_user 后的 get_currentuserinfo();然后通过 $user_ID = $current_user->ID 获取当前用户 id。有关获取当前用户登录的更多信息,请参阅http://codex.wordpress.org/Function_Reference/get_currentuserinfo

    【讨论】:

      【解决方案4】:

      你应该用'将值括起来

      像这样:

      $wpdb = "DELETE FROM wp_posts WHERE post_author = '$userID';"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-21
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多