2013-03-27 51 views
-1

我有这样的代码在我的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

定义了“$ wpdb”的位置? – chriz 2013-03-27 17:06:15

+0

你有什么错误信息? – 2013-03-27 17:06:47

+0

可能重复[在这个查询中有什么问题,它会删除表的所有记录!](http://stackoverflow.com/questions/5018131/what-is-the-problem-in-this-query-it-将删除所有记录的表) – mario 2013-03-27 17:09:07

回答

1

删除应该写入不带*字符。

0

查询应该是这样的:

DELETE FROM table WHERE ....; 
你的情况

"DELETE FROM wp_posts WHERE post_author = $userID;" 
+0

这几乎可以解答它。您可以参考http://www.w3schools.com/sql/sql_delete.asp以获取更多信息以及将来的参考。 – Chikilah 2013-03-27 17:08:50

0

你应该附上与'价值

像这样:

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