我正在尝试创建一个帖子,如果它不存在,并根据其标题更新帖子(如果存在)。标题是唯一的,因为它是用户的用户名。
但是,更新功能不起作用。始终会创建一个新帖子。
以下代码创建并应更新帖子:
<td><input type="submit" class="button button-primary" name="info_update_config" value="Save Changes" /></td>
<?php
function wp_exist_post_by_title($title)
{
global $wpdb;
$return = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = \'" . $title . "\' && post_status = \'publish\' && post_type = \'post\' ", \'ARRAY_N\');
if (empty($return)) {
return false;
} else {
return true;
}
}
if (isset($_POST[\'info_update_config\'])) {
$current_user = wp_get_current_user();
$user_id = get_current_user_id();
// usage
if (wp_exist_post_by_title($current_user->user_login)) {
$id = $wpdb->get_row("SELECT ID FROM wp_posts WHERE post_title = \'" . $title . "\' && post_status = \'publish\' && post_type = \'post\' ", \'ARRAY_N\');
$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);
// save a basic text value
$field_key = "field_577431181309c";
$value = $uids;
update_field($field_key, $value, $my_post);
} else {
$my_post = array(
\'post_title\' => $current_user->user_login,
\'post_content\' => $current_user->user_email,
\'post_status\' => \'draft\',
\'post_author\' => $user_id
);
// Insert the post into the database
$postID = wp_insert_post($my_post);
// save a basic text value
$field_key = "field_577431181309c";
$value = $uids;
update_field($field_key, $value, $postID);
}
}
//}
?>
有什么建议我做错了什么?
亲切的问候!