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 );
Related
For a custom post type in WordPress, I want to automatically use the post ID as a slug for the post. For this, I use the following code:
add_action( 'wp_insert_post', 'change_slug', 10, 3 );
function change_slug( $post_id, $post, $update ) {
if ( $post->post_type != 'custom_post_type' ) {
return;
}
wp_update_post(
array(
'ID' => $post_id,
'post_name' => $post_id, // slug
)
);
}
This code is based on a previous answer on so: https://wordpress.stackexchange.com/a/160483/181877
However, using this code I cannot even open the new post page for this CPT in WordPress. instead, I get a 503 Service unavailable message, which I believe is caused by a timeout.
I'm not sure what's wrong with my code. Any pointers?
Ok, I figured it out.
It seems like it resulted in an infinite loop, not sure why. But I included an extra if statement to check for the $update variable, making sure the code is not run during the update of a post and only when a new one is published. This seemed to have resolved it.
Below is my final code:
add_action( 'wp_insert_post', 'change_slug', 10, 3 );
function change_slug( $post_id, $post, $update ) {
if ( $post->post_type != 'custom_post_type' ) {
return;
}
if ( $update ) { // added this
return;
}
wp_update_post(
array(
'ID' => $post_id,
'post_name' => $post_id,
)
);
}
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 asked the programmer to have the title of every post automatically be the date selected and the place selected (to allow users to save some time). When I click 'publish' the title of the post is the date twice (ie; 03-26-15 03-26-15). Then the 'publish' button turns into an 'update' button. And then when I click 'update' the title will be correct (ie; 03-26-15 London). I'm trying to figure out how to get it this way after clicking 'publish' the first time. The programmer disappeared and I can't figure it out. Any help would be great.
function post_updated( $post_id ) {
global $post_type;
if ($post_type == 'place') {
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$postTitle.=get_the_date( 'm/d/Y', $post_id );
$postTitle.=" ".get_the_title(get_post_meta($post_id, 'place_locale', true) ) ;
$post['ID'] = $post_id;
$post['post_title'] = $postTitle;
remove_action( 'save_post', 'post_updated' );
wp_update_post($post);
add_action( 'save_post', 'post_updated' );
}
}
}
add_action( 'save_post', 'post_updated' );
The .= operator appends, therefor your titles are being duplicated.
Simply change it to = and you should be fine.
Are you sure this is the only function? It seems to only have "update post" code.
Post creation is handled with wp_insert_post something like:
$my_post = array(
'post_title' => $title,
'post_content' => "",
'post_status' => "pending",
'post_author' => wp_get_current_user()->ID
);
$post_id = wp_insert_post( $my_post );
Old question, but this may help who got into this question:
There are some problems with the code:
global $post_type is not defined.
As mentioned by #dojs, you should use = operator instead of .= to define the variable first.
get_the_title() function only accepts post_id or post object as the param.
Since you're dealing with a custom post type of place, it's better to use a post type specific hook instead of the general and crowded save_post hook.
After these modifications, the final code could be something like this:
function tst_post_updated( $post_id )
{
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$post_title = get_the_date( 'm/d/Y', $post_id );
$post_title .= " " . get_post_meta($post_id, 'place_locale', true);
$post_title = sanitize_title($post_title);
$post['ID'] = $post_id;
$post['post_title'] = $post_title;
remove_action( 'save_post_place', 'tst_post_updated' );
wp_update_post($post);
add_action( 'save_post_place', 'tst_post_updated' );
}
}
add_action( 'save_post_place', 'tst_post_updated' );
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
I originally wanted to be able to password protect a category. At the very least I wanted it to be password-protected, but preferably a username and password login. Since I have been unsuccessful for days at finding a solution I have now resorted to using WordPress's built-in password protection for posts.
The issue I am having is I will be posting via e-mail and in order to have these posts password-protected I need to login to Wordpress and then manually select password-protected and enter a password in the dashboard.
I would like to be able to have all posts that appear in a specific category be password-protected with the same password by default. Eliminating having to log in to Wordpress and manually select password protect.
I know there is a function <?php post_password_required( $post ); ?> that I need to use but I am not sure how to implement it or where.
Based on this WordPress StackExchange answer. Tested only with a regular dashboard. Publishing via email has to be tested, but I suppose the hook gets called in this kind of posting.
add_action( 'save_post', 'wpse51363_save_post' );
function wpse51363_save_post( $post_id ) {
//Check it's not an auto save routine
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
//Check it's not an auto save routine
if ( wp_is_post_revision( $post_id ) )
return;
//Perform permission checks! For example:
if ( !current_user_can( 'edit_post', $post_id ) )
return;
$term_list = wp_get_post_terms(
$post_id,
'category',
array( 'fields' => 'slugs' )
);
if( in_array ( 'the-category-slug', $term_list ) )
{
// Unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'wpse51363_save_post' );
// Call wp_update_post update, which calls save_post again.
wp_update_post( array(
'ID' => $post_id,
'post_password' => 'default-password' )
);
// Re-hook this function
add_action( 'save_post', 'wpse51363_save_post' );
}
}
add_filter( 'wp_insert_post_data', function( $data, $postarr ){
if ( 'book' == $data['post_type'] && 'auto-draft' == $data['post_status'] ) {
$data['post_password'] = wp_generate_password();
}
return $data;
}, '99', 2 );