I’m struggling to understand some confusing behavior with wp_update_post: a direct copy/paste of content completely corrupts a bunch of Gutenberg plugins.
Example:
// Get original post
$my_post = get_post(POST_ID);
$my_post_content = $my_post->post_content;
// Copy the content and update the post with the same content
$copy_post = array();
$copy_post[‘ID’] =POST_ID;
$copy_post[‘post_content’] = $my_post_content;
wp_update_post($copy_post);
If you do the above on a post that has a PostX Post Grid, for example, the PostX grids on the page will all be corrupted and broken. But I’m not sure why copying and pasting the post content should change anything.
What am I missing? Thanks!
The page I need help with: https://pastebin.com/79wVp1qB
]]>Example:
// Get original post
$my_post = get_post(POST_ID);
$my_post_content = $my_post->post_content;
// Copy the content and update the post with the same content
$copy_post = array();
$copy_post['ID'] =POST_ID;
$copy_post['post_content'] = $my_post_content;
wp_update_post($copy_post);
If you do the above on a post that has a PostX Post Grid, for example, the PostX grids on the page will all be corrupted and broken. But I’m not sure why copying and pasting the post content should change anything.
What am I missing? Thanks!
]]>regards
]]>I cant get my function work. I searched some threats, and they say that it wont work.
I just want to transfer a custom field to post_excerpt if the post is saved or updated.
This is the code I found and tested, but it only works if I save the Post 2 times.
function my_function( $post_id ){
// Only set for post_type = post!
/*if ( 'citadela-item' !== $post->post_type ) {
return;
}*/
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'my_function');
$address_to_excerpt = get_post_meta( $post_id, '_city_address', true );
// update the post, which calls save_post again
$my_args = array(
'ID' => $post_id,
'post_excerpt' => $address_to_excerpt,
);
wp_update_post( $my_args );
// re-hook this function
add_action('save_post', 'my_function');
}
}
add_action( 'save_post', 'my_function' );
Now I found this ACtion wp_after_insert_post
but I dont understand how it works. there are no examples I understand. But it seems to be the right action.
Any suggestions to help?
Thanks,
Denis
As of right now, I have a function that does the work properly but it requires that the post be re-loaded in order for the correct data to be displayed.
This is accomplished using some php I’ve added to my single.php file right before the Loop.
simplified example:
<?php if(function_exists('price_updater')) { price_updater(); } ?>
‘price_updater’ uses ‘wp_update_post’ to update the content and a meta_field. These definitely get updated but I get the previous version on the first pass. If I hit the reload page it shows the updated version.
So the question is, how do I set this up to avoid refreshing the page?
I’m guessing using ‘add_action’ or ‘add_filter’ but I’ve had no success so far – the handful I’ve tried either have the same problem or do nothing.
]]>Firstly, thanks for wonderful plugin, it helps me so much:)
I’m trying to create CPT’s posts via wp_insert_post and update post via wp_update_post() on API call from another site(that site returns ID of post which I need to update).
so all works good except of custom taxonomy terms.
I have 4 custom taxonomies but I’ll show you only 2 for example.
Now I have only 2 languages – English(default) and Russian.
// $sValuesArray is demo data
$sValuesArray = array('industry1', 'industry2');
$industries = wp_set_object_terms( $post_id, $sValuesArray, 'industries', true);
foreach ($industries as $industry) {
pll_set_term_language($industry + 1, 'ru');
}
// $branchesArr is demo data
$branchesArr = array('branch1','branch2');
$branches = wp_set_object_terms($post_id, $branchesArr, 'branches', true);
foreach ($branches as $branch) {
pll_set_term_language($branch + 1, 'ru');
}
I know that is pll_set_term_language($branch + 1, 'ru');
– bad practice.
I do it because terms which created returns (for example) ID 1109, but in admin dashboard that ID has 1110, so for attach created russian term to ‘RU’ language I’m using this fix. Could you please provide better solution? thanks.
My main trouble:
Why created terms not attaching to my post? It’s just created, but not attached.
But if I send in $branchesArr
and sValuesArray
existing terms all works good.
If I disable polylang – all works good too.
Thanks for plugin and your help.
]]> $update_slug = array(
'ID' => $post_id,
'post_name' => get_the_title( $post_id )
);
wp_update_post( $update_slug );
Thanks for some clearing answers,
Cheers,
Denis
I like to update the POST_NAME after I updated the post title. This seems not to work automaticly.
I use this function to update the POST_NAME. As far as I can see, this worsk very well. Or do I miss something important? Maybe I can delete the sanitize_title if the wp_update_post does it on its own.
$my_post = array(
'ID' => $post_id,
'post_name' => sanitize_title( get_the_title( $post_id ) )
);
wp_update_post( $my_post );
Thanks for help,
Denis
functions.php
a function which, when a post in a certain category is created/edited, will create also a related page or update it if already exists.
The creation of a new page with wp_insert_post
works like a charme, but if the page already exists (after else
statement) and use wp_update_post
instead, WordPress returns just a blank screen and doesn’t update the post and neither the page.
I can’t figure out why, define('WP_DEBUG', true);
doesn’t return any hint.
`$page_check = get_page_by_title($promo_page_title);
if(!isset($page_check->ID)){
$new_page = array(
‘post_type’ => ‘page’,
‘post_title’ => $promo_page_title,
‘post_content’ => $promo_page_content,
‘post_status’ => ‘publish’,
‘post_author’ => 1,
‘post_parent’ => 27
);
$new_page_id = wp_insert_post($new_page);
if($new_page_id){
update_post_meta($new_page_id, ‘_wp_page_template’, ‘page_promo.php’);
$pids[0] = $new_page_id;
} else {
$e = TRUE;
wp_die(‘Error creating promo page’);
}
}
else {
$new_page = array(
‘ID’ => $page_check->ID,
‘post_type’ => ‘page’,
‘post_title’ => $promo_page_title,
‘post_content’ => $promo_page_content,
‘post_status’ => ‘publish’
‘post_author’ => 1,
‘post_parent’ => 27
);
$new_page_id = wp_update_post( $new_page );
if($new_page_id){
update_post_meta($new_page_id, ‘_wp_page_template’, ‘page_promo.php’);
$pids[0] = $new_page_id;
} else {
wp_die(‘Error updating promo page’);
}
}
unset($promo_page_content);`
Thanks for your help!
]]>