Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 http://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...

share|improve this question
    
I'm not really grasping what you are doing, but is there a way to pass the post ID nto wp_update_post? codex.wordpress.org/Function_Reference/wp_update_post –  Rev. Voodoo Jun 10 '11 at 19:30

2 Answers 2

up vote 2 down vote accepted

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().

share|improve this answer
    
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. –  dwenaus Jun 10 '11 at 19:51
1  
and why are you using useraidi and not user_id. odd. –  dwenaus Jun 10 '11 at 19:53
1  
Because you needed something else than the actual logged author. Besides, I'm not a fan of using too default name variables, never knows. –  Álex Acuña Viera Jun 10 '11 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. –  kaiser Jun 10 '11 at 21:12
    
@kaiser fixed the array thing. –  Álex Acuña Viera Jun 10 '11 at 21:23

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.