Exemple wp_update_post () ... comment mettre à jour the_content dans une zone de texte?
-
-
où utilisez-vous ce code?where are you using this code?
- 0
- 2011-07-07
- Hameedullah Khan
-
le haut de content-pt-album.php,pt étant letype depublication.Estmodelé après vingt onze.the top of content-pt-album.php, pt being post type. Its modeled after twentyeleven.
- 0
- 2011-07-07
- m-torin
-
3 réponses
- votes
-
- 2011-07-23
Cela dépend de l'endroit où vous l'utilisez.Global $post vous donne-t-il lemessage spécifique que vous souhaitezmettre àjour?Votre codepostal demise àjour Wordpressme semble correct,mais l'instructionif est-elle valideet $post-> ID renvoie-t-il lebonint?
It depends on where you're using this. Is global $post giving you the specific post that you want to update? Your Wordpress update post code looks right to me, but is the if-statement valid, and is $post->ID yielding the correct int?
-
Oui,$postet $post-> IDproduisent lesbons résultats.:)Yes, $post and $post->ID are producing the right outputs. :)
- 0
- 2011-07-24
- m-torin
-
- 2016-09-27
J'ai lemêmeproblème. Mon codeest dans lefichier
single.php
. J'utilise le code de cet article: Modification de l'articlefrontal à l'aide d'unformulaireAprès avoir cliqué sur soumettre,le code dans
single.php
exécutewp_update_post()
avec l'ID depublication renvoyé. Puisqu'ilestexécuté àpartir d'unfichiermodèle,le wp_query a déjà été rempli afin que lapage restetoujours avec les anciennes données depublication. Sij'actualise sans soumettre,lesnouvelles données sont renseignées.Jene suispas sûr que ce soit lameilleure solution,mais celafonctionne. Après l'exécution de
wp_update_post()
,j'écrase la variableglobale$wp_query
en utilisant lamême requête qui a étéexécutée avant l'appel de cefichiermodèle.global $wp_query; if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['post_id']) && !empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['post_content'])) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ('page' == $post_type) ? 'edit_page' : 'edit_post'; if (current_user_can($capability, $post_id) && wp_verify_nonce($_POST['update_post_nonce'], 'update_post_' . $post_id)) { $post = array( 'ID' => esc_sql($post_id), 'post_content' => wp_kses_post($_POST['post_content']), 'post_title' => wp_strip_all_tags($_POST['post_title']) ); $result = wp_update_post($post, true); if (is_wp_error($result)){ wp_die('Post not saved'); } $wp_query = new WP_Query($wp_query->query); //resets the global query so updated post data is available. } else { wp_die("You can't do that"); } }
J'aiessayé d'appeler
wp_reset_postdata()
etwp_reset_query()
à laplace,maisje suppose qu'il s'agit d'une réinitialisation sur une copieen cache carje reçoistoujours les anciennes données depublication .Une autre solution qui afonctionné était d'obtenir l'URL actuelleen utilisant:
global $wp; $current_url = home_url(add_query_arg(array(),$wp->request));
puis après
wp_update_post()
:wp_redirect($current_url);
Le code de $ current_url a été trouvéici.
I'm getting the same problem. My code is in the
single.php
file. I'm using the code from this article: Front end post editing using a formAfter clicking on submit, the code in
single.php
does runwp_update_post()
with the post ID being returned. Since this is being run from a template file, the wp_query has already been populated so the page still renders with the old post data. If I refresh without submitting, the new data is populated.I'm not sure if this is the best solution for it, but it works. After
wp_update_post()
is run, I overwrite the global$wp_query
variable using the same query that was run before this template file was called.global $wp_query; if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['post_id']) && !empty($_POST['post_title']) && isset($_POST['update_post_nonce']) && isset($_POST['post_content'])) { $post_id = $_POST['post_id']; $post_type = get_post_type($post_id); $capability = ('page' == $post_type) ? 'edit_page' : 'edit_post'; if (current_user_can($capability, $post_id) && wp_verify_nonce($_POST['update_post_nonce'], 'update_post_' . $post_id)) { $post = array( 'ID' => esc_sql($post_id), 'post_content' => wp_kses_post($_POST['post_content']), 'post_title' => wp_strip_all_tags($_POST['post_title']) ); $result = wp_update_post($post, true); if (is_wp_error($result)){ wp_die('Post not saved'); } $wp_query = new WP_Query($wp_query->query); //resets the global query so updated post data is available. } else { wp_die("You can't do that"); } }
I've tried calling
wp_reset_postdata()
andwp_reset_query()
instead but I'm guessing it is resetting to a cached copy because I still get the old post data.Another solution that worked was getting the current url using:
global $wp; $current_url = home_url(add_query_arg(array(),$wp->request));
and then after
wp_update_post()
:wp_redirect($current_url);
The code for the $current_url was found here.
-
L'utilisation de `$post` comme variableestprobablement votreproblème.Using `$post` as a variable is probably your problem.
- 0
- 2020-03-26
- Tim Hallman
-
- 2020-04-07
Vouspouvez remplacer lespropriétés
WP_Post
et lesenvoyer àwp_update_post
:/** @var WP_Post $post */ $post = get_post( 123 ); $post->post_content = "Some other content"; wp_update_post( $post );
Jetrouve celaplusfacile qu'untableau.
You can override the
WP_Post
properties and send it towp_update_post
:/** @var WP_Post $post */ $post = get_post( 123 ); $post->post_content = "Some other content"; wp_update_post( $post );
I find it easier than an array.
J'utilise lafonction ci-dessouspourmettre àjour laméta de l'article depuis lefront-end. Commentpuis-je ajouter aumieux une zone detexte quimet àjour
the_content()
en utilisantwp_update_post()
?-
Donc,cetextraitpublie desmodifications dans labase de données,mais lorsque lapage s'actualise lors de la soumission,l'ancien
the_content()
est affiché. Lemessage doit être actualisémanuellementpour voir lesmodifications.Monextrait de codeest-ilmalformé?