2013-01-23 57 views
-1

我想要特定用戶獲得發佈最後修改/特定日期,然後回顯它。Wordpress,從sql數據庫中獲取查詢並回應它

我試圖做的是:

<?php $id = get_the_ID();    

global $current_user; 
$current_user = wp_get_current_user(); 

$postID = $id; //assigning post id 
$userID = $current_user->ID; // assigning user ID 

$sql = "SELECT post_date FROM wp_posts WHERE ID = $postID AND post_author = $userID ORDER BY post_date DESC LIMIT 1"; 
$userModifiedDate = $wpdb->query($sql); 
echo $userModifiedDate; ?> 

哪裏是我的錯?任何人都可以帶領我通過這個?

目前$userModifiedDate返回我1

回答

1

$wpdb->query()返回受影響的行數而不是實際的查詢結果。

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

嘗試使用更具體的功能,如$wpdb->get_var()$wpdb->get_results()

$userModifiedDate = $wpdb->get_var($sql); 

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

此外,雖然它不是絕對必要,我總是喜歡通過任何疑問通過$wpdb->prepare()第一個:

$sql = $wpdb->prepare("SELECT post_date FROM wp_posts WHERE ID = %d AND post_author = %d ORDER BY post_date DESC LIMIT 1", $postID, $userID); 

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

+0

非常感謝你,到tweek它一點點需要,但對我幫助很大! :) –