I'm using PHP to dynamically create a custom post, and I need the author to be someone other than the logged in user. I found this https://stackoverflow.com/questions/5759359/wordpress-manually-set-the-author-of-a-post-in-php but I'm wondering if there is a way to do it after the post is already inserted. I guess I could just do a db query...
3 Answers
If you know the ID of the author you can use wp_insert_post specifying the ID and the author ID to it.
$id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
The trick is to specify the ID to update the post. See wp_insert_post()
.
-
i was looking at wp_insert_post and thought that the user_id was hard coded. but it was just set as the default. and maybe it was another bug that caused it not to work. duh! thanks.– dwenausCommented Jun 10, 2011 at 19:51
-
1
-
1Because you needed something else than the actual logged author. Besides, I'm not a fan of using too default name variables, never knows. Commented Jun 10, 2011 at 20:20
-
But what about
arrayh();
? This won't ever work... Plus: Using localized names makes it hard for others to understand your code. This is imo more important if you're answering on a public Q/A site that should be a reference for thousands of people.– kaiserCommented Jun 10, 2011 at 21:12 -
For simplicity and relevancy between this question and another question asked on Stack Overflow (wordpress - manually set the author of a post in php -- As linked by OP here on WPSE.).
WordPress seems to force a value for the post_author
while inserting or updating posts using wp_insert_post()
and wp_update_post()
.
The way around it is to use the filter hook wp_insert_post_data
.
/**
* Filter slashed post data just before it is inserted into the database.
*
* @since 2.7.0
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
Example Usage of filter hook wp_insert_post_data
:
function remove_author_id( $data, $postarr ) {
if ( $data['post_type'] != 'YOUR-POST-TYPE-HERE' ) {
return $data;
}
$data['post_author'] = 0;
return $data;
}
add_filter( 'wp_insert_post_data', 'remove_author_id', '99', 2 );
This can be particularly nice for inserting or updating posts using PHP
.
Note: You will want to be sure to disable the support for author
in your Custom Post Type and probably be cautious using any author related functions within the scope of this Post Type.
If this is a custom post type and you don't want an author assigned to a post you can remove 'author' from supports( array )
in register_post_type. http://codex.wordpress.org/Function_Reference/register_post_type
If you still need author support for your post type, this would make much more sense to just do this in post.php / post-new.php by filtering the author metabox.
The solution is to add a none or null user to the dropdown using wp_dropdown_users 'show_option_none'
WordPress will use <option value="-1">
for your null user but it will show up as 0 in the db.
*Note: This example also moves the author div right above the publish button.
add_action( 'post_submitbox_misc_actions', 'move_author_meta' );
function move_author_meta() {
global $post_ID;
$post = get_post( $post_ID );
echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
better_author_meta_box( $post ); //This function is being called in replace author_meta_box()
echo '</div>';
}
function better_author_meta_box($post) { ?>
<label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
<?php
if ( 'auto-draft' == $post->post_status ) : $selected = false; elseif ( $post->post_author == 0 || ( ! $post->post_author) ) : $selected = -1; else : $selected = $post->post_author; endif;
wp_dropdown_users( array(
'who' => 'authors',
'name' => 'post_author_override',
'selected' => $selected ? $selected : (int) -1,
'include_selected' => true,
'show_option_none' => 'NONE',
'orderby' => 'display_name',
'show' => 'display_name',
'order' => 'ASC'
) );
}
Im sure you notice all the extra conditional checks for $selected. It might be overkill but eliminated any issues with editors not being able to change an author to none from previous published posts.