我正在使用以下功能。我想更新标题中包含currentUserName
的帖子。
我正在做以下事情:
$id = (int)$wpdb->get_row( "SELECT ID FROM wp_posts WHERE post_title = '" . $current_user->user_login . "' && post_status = 'draft' && post_type = 'post' ", 'ARRAY_N' );
// Update post
$my_post = array(
'ID' => $id,
'post_title' => $current_user->user_login,
'post_content' => $current_user->user_email,
);
// Update the post into the database
wp_update_post( $my_post );
但是,我得到的$id
是1
,因此帖子没有更新。由于错误,我得到了:
帖子ID无效。
有什么建议我做错了吗?
答案 0 :(得分:1)
使用get_var从db获取单个值,并且' AND'在mysql中
$id = (int)$wpdb->get_var( "SELECT ID FROM wp_posts
WHERE post_title = '" . $current_user->user_login . "'
AND post_status = 'draft'
AND post_type = 'post' ", 'ARRAY_N' );
答案 1 :(得分:1)
$row = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = '" . $current_user->user_login . "' && post_status = 'draft' && post_type = 'post' ", 'ARRAY_N');
if (!empty($row)) {
// Update post
$my_post = array(
'ID' => $row[0],
'post_title' => $current_user->user_login,
'post_content' => $current_user->user_email,
);
// Update the post into the database
wp_update_post($my_post);
}
试试这个代码段