Recibo un error con la función wordpress wp_update_post () que dice ID de publicación no válida. Aqui esta mi codigo
$current_item = 273;
$my_post = array(
"ID" => $current_item,
"post_title" => "This is the post title.",
"post_content" => "This is the updated content.",
);
$post_id = wp_update_post( $my_post, true );
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}
Gracias por adelantado .
Respuestas
2 para la respuesta № 1Utilizar "import_id"
no "ID"
.
Si no hay ninguna publicación en la ID que especifique, wp_update_post()
no crea uno nuevo; devuelve un error. Para especificar el ID de un nuevo uso posterior "import_id" => $current_item
.
Tenga en cuenta, sin embargo, que si hay una publicación con esa ID, import_id
provocará una nueva publicación en lugar de una actualización. Por lo tanto, si desea hacer una nueva publicación con esa ID O actualizar la publicación en esa ID, necesitará un if
declaración para elegir su clave:
$newPostKey = (get_post_status($current_item)) ? "ID" : "import_id";
// If there"s a post with an ID of $current_item, we"ll use "ID".
// Otherwise, use "import_id".
Aquí está tu nuevo y brillante código.
$current_item = 273;
$newPostKey = (get_post_status($current_item)) ? "ID" : "import_id";
$my_post = array(
$newPostKey => $current_item,
"post_title" => "This is the post title.",
"post_content" => "This is the updated content.",
);
$post_id = wp_update_post( $my_post, true );