-1

我想获得特定用户的最后修改/创建日期,然后回显它。

我想做的是:

<?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

4

1 回答 1

1

$wpdb->query()返回受影响的行数,而不是实际的查询结果。

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

尝试使用更具体的功能,例如$wpdb->get_var()or $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

于 2013-01-23T18:16:03.857 回答