Custom field value set to permalink only on new post, But they change it every time it's updated - wordpress

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}
};
};
};

Related

save_post_post-type WordPress Hook, only works when i create a new post but it doesnt work when i edit a post

I have a Custom Post Type "Compania", it has a meta field called "compania_id" i would like that field = current post_id
I implemented this action to do that and it works when i create a new post (compania_id automatically is equal to current post_id) but in older compania post when this code hasnt been created, when i edit these post and save, compania_id isnt filled with the post_id as when i create a new post, why??
function anadir_post_id($post_id){
update_post_meta($post_id, 'compania_id', $post_id);
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 1);
The hook you use has three parameters. Therefore your code needs to look like this.
function anadir_post_id( int $post_ID, WP_Post $post, bool $update ){
update_post_meta( $post_id, 'compania_id', $post_id );
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 3 ); //<--- 3 params!
If you have an older version of php it will look like this:
function anadir_post_id( $post_ID, WP_$post, $update ){
update_post_meta( $post_id, 'compania_id', $post_id );
}
add_action( 'save_post_compania', 'anadir_post_id', 10, 3 ); //<--- 3 params!
And, of course, if you have access to a post's wp_postmeta rows, you already know its ID. What you store here may be redundant.

WP Update post to again update the current posr

I have a post type called pdf files with a custom metabox pdf fic /
When I save post I want to update current post title with the pdf path.
My function use the hook save post :
function set_private_categories($post_id) {
if ( $parent_id = wp_is_post_revision( $post->ID ) )
$post_id = $parent_id;
remove_action( 'save_post', 'set_private_categories' );
$fic = get_post_meta($post_id, 'wpcf-fichier-pdf', true);
wp_update_post( array( 'ID' => $post_id,'post_title'=>$fic, 'post_status' => 'private' ) );
add_action( 'save_post', 'set_private_categories' );
}
add_action( 'save_post', 'set_private_categories' );
My problem :
I have to update twice before the title is replaced by the pdf metabox input value, it seems that wordpress does not update the database with the new value of the metabox.
How to achieve this please? Thoughts?
Use $POST['wpcf-fichier-pdf'] to grab the meta value before the post is saved. Make sure to sanitize the value before saving to the database.

can i call function in my wordpress plugin when sending publish post in every plugin or user?

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 );

Wordpress - Get Attachment url when publish post

I have a problem. I want to get the featured image url when a post was published.
It works when I update a post, but not when it was published for the first time, because the meta data seems not to be stored in the database at this moment. Even when I use 'wp_insert_post' instead of 'save_post' it does not work.
In my functions.php i check for new/updated posts with:
add_action( 'save_post', 'my_function' );
When a post was updated I read the featured image url by using:
$image_url = get_post_meta( get_post_meta( $post_id, "_thumbnail_id", true ), "_wp_attached_file", true );
Can you help me?
Well, if you want to take attachments from the post you are publishing, save_post is a no go.
Try publish_post
At the moment when publish_post is fired, the post and its attachments already exists in the database and can be accessed.
save_post action hook runs after the data is saved to the database ( wordpress codex ), so this should do it, it works on post publish and post update. A couple of useful links is commented within the code.
// http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
add_action( 'save_post', function ( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// save_post action is executed on all post actions like publish, trash, auto-draft, etc.
// http://codex.wordpress.org/Post_Status_Transitions
if ( get_post_status( $post_id ) == 'publish' ) {
// http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id
$thumb_id = get_post_thumbnail_id( $post_id );
// http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
$thumb_url = wp_get_attachment_image_src( $thumb_id, 'full' );
$thumb_url = $thumb_url ? $thumb_url[0] : false;
}
});

wordpress set post_status as "draft" in 'save_post' action

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

Resources