wordpress – manually set the author of a post in php

I’m using a wordpress theme that allows users to create posts (they don’t need to be logged in). The problem is that when the posts are displayed, <?php the_author(); ?> always shows as “admin”. The post-creation form has a name field that I can get at in functions.php, but how can I manually change the author of the post to the name that was entered in the form field?

Related posts

Leave a Reply

2 comments

  1. Without seeing the context, I can only offer a step in the right direction. Within a plugin or functions.php in your theme, you can call on the wp_insert_post_data filter to change the data that will be inserted as your post. For instance, if you wanted every post to be attributed to author with ID 5, you could do something like:

    function my_change_author( $data , $postarr )
    {
      $data['post_author'] = 5;
      return $data;
    }
    
    add_filter('wp_insert_post_data' , 'my_change_author' , '99', 2);
    

    I’ve done something like this before where I have front end forms that I capture as posts and automatically assign to a predefined author that I call Web Monkey or something like that 😉 Check out this page for more: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data. This filter is really powerful in that you can alter your data that will become your post in anyway before it hits the database.

  2. Have you checked what the_author_meta() contains? WP has to attach a userID to each post, so chances are it’s assigning user 1 to those posts regardless of what they’re entering into the box. Without seeing the custom function or knowing what theme you’re using to test, it’ll be pretty difficult to troubleshoot.