2013-03-27 53 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';"