我在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文件中定义的
答案 0 :(得分:1)
删除时应该没有*字符。
答案 1 :(得分:0)
查询应该是这样的:
DELETE FROM table WHERE ....;
在你的情况下:
"DELETE FROM wp_posts WHERE post_author = $userID;"
答案 2 :(得分:0)
您应该使用'
将值括起来像这样:
$wpdb = "DELETE FROM wp_posts WHERE post_author = '$userID';"
答案 3 :(得分:0)
尝试使用$ 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