I have a custom function that works with my custom post type. While porocessing save_post action:
add_action( 'save_post', 'my_custom_function' );
I would like to set post status as draft (in case of a problem with getting custom data from outside api).
In my my_custom_function function I have this little block:
if ($error == true) {
$override_post = array();
$override_post['ID'] = $post_id;
$override_post['post_status'] = 'draft';
wp_update_post( $override_post );
}
but it seems, that after save_post is being processed, then post_status is being set again.
Anybody have an idea, where should I hook into, so while saving post data I can modify its post_status, post_date and some other post data informations so they are not being overriten?
You should hook it to wp_insert_post_data. Then you could use a function like this to set your post status to draft:
add_filter( 'wp_insert_post_data', 'set_post_to_draft', 99, 2 );
function set_post_to_draft( $data, $postarr ) {
if ( your_condition ) {
$data['post_status'] = 'draft';
}
return $data;
}
I had to make a post type with only one post_status option, and it seem to fit your needs too, as it is works exactly with the save_post hook.
add_action( 'save_post', 'my_function' );
function my_function( $post_id ){
if ( ! wp_is_post_revision( $post_id ) ){
// avoid endless circle
remove_action('save_post', 'my_function');
// update the data before saving
wp_update_post( wp_slash([
'ID' => $_POST['ID'],
'post_status' => 'draft'
]));
// restore the saving hook
add_action('save_post', 'my_function');
}
}
The original solution found here:
https://wp-kama.ru/function/wp_update_post
Related
I want to set the permalink slug using the custom field value for the first save only, but it is not working.
The code below changes the slug not only for the first save, but also for every update.
function custom_slug_auto_setting( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
return $_POST['custom_field_title'];
}
add_filter( 'wp_unique_post_slug', 'custom_slug_auto_setting', 10, 6 );
For the second and subsequent saves, I want to keep the slug set for the first save.
I tried using the filter hook for wp_insert_post to specify post_name only for the first save, but that didn't work well either.
Is there any good solution?
Thank you.
save_post or save_post_{$post->post_type} Fires once a post has been saved. The dynamic portion of the hook name, {$post->post_type}, refers to the post type slug.
We need to discard any updates autosave or revision actions. The WordPress autosave system fire every 60 seconds.
#See https://wordpress.org/support/article/revisions/
The $update parameter is supposed to determined whether this is an existing post being updated. It applies more specifically to a post autosave revision. The $update parameter will always be true when firing through wp_publish_post. But that isn't true for its usage in wp_insert_post (See the following wordpress stackexchange answer and comments for more details...).
#See https://wordpress.stackexchange.com/a/185991/190376
In our case, the wp_publish_post function publish a post by transitioning the post status.
#See https://developer.wordpress.org/reference/functions/wp_publish_post/
By additionally crosschecking the post status we can effectively determine whether it is indeed a non-existing post.
If you are calling a function such as wp_update_post that includes the save_post hook, your hooked function will create an infinite loop. To avoid this, unhook your function before calling the function you need, then re-hook it afterward.
#See https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops
save_post will fire on post Update (eg: Autosave) or Publish which is why we're runing into an infinite loop.
#See https://wordpress.stackexchange.com/a/19539/190376
<?php
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
if ( ! function_exists( 'wpso74121743' ) ) {
function wpso74121743( $post_id, $post, $update ) {
if ( ! wp_is_post_autosave( $post_id ) && ! wp_is_post_revision( $post_id ) && ! $update && $post->post_status == 'auto-draft' ) {
$override_post_name = sanitize_title( get_post_custom_values( 'custom_field_title', $post_id ), get_the_title( $post_id ) );
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
wp_update_post( array(
'ID' => $post_id,
'post_name' => $self,
) );
add_action( 'save_post', 'wpso74121743', 10, 3 ); //can be replaced by save_post_{$post->post_type}
};
};
};
I'd like to update post_author after update post by getting current user ID, but when I use this function wordpress created new post with correct author by copied current post
function change_author () {
if ( ! wp_is_post_revision( $post_id ) ){
$post= array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
wp_update_post( $post );
}
}
add_action('save_post', 'change_author');
I'd use the wp_insert_post_data hook instead of save_post. Rather than updating the post after it's saved, it's probably better to change the data before it gets inserted into the database as part of the save post action. Here's what I'd do:
function change_author ( $data ) {
if ( ! wp_is_post_revision( $data['ID'] ) ){
$data['post_author'] = get_current_user_id();
}
return $data;
}
add_filter( 'wp_insert_post_data', 'change_author', 10, 1 );
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr ) {
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
}
but it works only on first insert, when I edit it the visibility change to public...
You can hook to the save_post which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update ){
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) ) {
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update ) {
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
} else {
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
}
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
}
}
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
Slight variation from your question, but you can still edit the post on creation, if you make all your post type private. Thus this works.
function force_type_private($post)
{
if ($post['post_type'] == 'Your Post Type')
$post['post_status'] = 'private';
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
I am using this hook to run some code after the product is updated:
add_action( 'updated_post_meta', 'attach_variation_images_on_product_save', 10, 4 );
function attach_variation_images_on_product_save( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_edit_lock' ) {
if ( get_post_type( $post_id ) == 'product' ) {
//do something
}
}
}
This is working as expected, the function is executed after the product is updated. I want to run the same function when a product is getting updated via the REST API. I hooked my function to woocommerce_rest_insert_product_object like this but it did not work:
add_action( 'woocommerce_rest_insert_product_object', 'attach_variation_images_on_product_update_via_rest', 10, 3 );
function attach_variation_images_on_product_update_via_rest( $post, $request, $true ) {
if ( get_post_type( $post ) == 'product' ) {
$product = wc_get_product( $post );
//do something
}
}
Am I not using the right hook? Is there another hook I can use?
EDIT 1: It seems that my code was not running because get_post_type($post) is type of post and not product. I am trying to attach an image to variations using add_post_meta($variation_id, '_thumbnail_id', $image_id); inside a loop. It seems the function attach_variation_images_on_product_update_via_rest( $post, $request, $true ) is executed till the end but it does not attach the image to the variations.
I ended up using this hook add_action( 'woocommerce_update_product', 'my_callback_function', 10, 1 ); that executes the callback function when you updating products from Woocommerce back-end as well from the REST API.
i want to call my function when user or admin or another plugin sending publish post.
any hook exist for this ?
i try some hook like publish_post.
but this work just if i put my code in that plugin which sending post.
i want my plugin Independent and when sending post in any plugin or admin or every where .... , my function in my plugin do something.
this is for SEO reason and its hard to say why i want do this.
some code i write
function post_published_notification($post) {
$ID= $post->ID;
$my_post = array(
'ID' => $ID,
'post_title' => "anything"
);
wp_update_post( $my_post );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );
There's are hook called save_post - do_action( 'save_post', $post_ID, $post, $update );. You could check if $post->post_status is set to published and maybe keep your own record of whether it's had the published status before.
Example on how to use it:
function post_published_notification( $post_ID, $post, $update ) {
if ( $post->post_status == 'publish' && ! get_post_meta( $post_ID, 'published_notification_set', true ) ) {
update_post_meta( $post_ID, 'published_notification_set', 1 );
}
}
add_action( 'save_post', 'post_published_notification', 10, 3 );