0

I allow users to post from frontend. There are three kinds of users. Myself as admin, a junior ( given a role of an author) and anyone from public as non-logged-in user. Currently if any non-logged-in user posts from frontend, I am assigned as an author with this code. Currently my wp_insert_post array looks something like this :

'post_title'    => $final_title,
'post_content'  => $about_stuff,
'post_status'   => 'draft',
'post_author'   => '20',
'post_type' => 'post',
 etc....

wherein my author id is '20'. All this works fine. Now, what I would like to achieve is when my junior logs-in and creates post in the frontend, I would like him to be the post author, and not me (as currently set). I understand that the post_author will need to be a variable eg. $content_creator. Given below is the code I have written till now, but I am confused how to put it together in order to achieve what I need. To be specific I am confused how do I make the variable $content_creatorwork with rest of the code.

if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if ( 19 == $current_user->ID ) {
    'post_author' => '19';
    } else {
    'post_author' => '20',
    }
 } 

The code is pretty self-explanatory and basic which says check if the user if logged in, if yes check the user-id. If user id equals 19 set 'post_author' => '19' (user id of my junior) otherwise set the author as admin. Two things I wanted to ask, do I also need global $post before my code and should I be using another wp_update_post filter instead. Please help.

Final scenario must be, when the admin or anyone else creates a post the post-author must be set as admin (me) but when my junior creates a post he must be set as the post-author. This would have been unnecessary if we were creating posts in the backend but due to some reason we prefer to create posts in frontend.


UPDATE : SOLVED Turns out it was pretty simple with wp_update_post. Although, now it works fine. Let me know if you find something funny. Here is the code I am using after wp_insert_post

if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if ( 19 == $current_user->ID ) {

    $update_creator = array(
                'ID'          => $pid, //created post ID
                'post_author' => '19'
            );

    wp_update_post( $update_creator );
    }
}
  • 1
    Please add this as answer and not as update your question. Thanks. – kaiser Dec 14 '13 at 13:23
  • alrite @kaizer. – gurung Dec 14 '13 at 13:49
1

The wp_insert_post_data filter is the right approach. I would do something like this:

add_filter( 'wp_insert_post_data', 'some_handler' );

function some_handler( $data )//$data is an array of sanitized post data
{

 //do some conditional checking...
 if ( ! isset( $_POST ) ) {//the request to insert post data wasn't done by a user

  return $data;
 }

 //don't bother doing anything to any post type other than 'post'    
 if ( $data['post_type'] != 'post' ) {

  return $data;
 }
 //there probably needs to be a bunch of other conditional checks made...
 //but after that, check for which author is posting:
 if ( $data['post_author'] == 19 ) {

  return $data;
 } else {

  $data['post_author'] = 20;
  return $data;
 }
}
  • I tried this, but found a bit complicated to employ. But many thanks for the effort. Voteup for the effort. – gurung Dec 14 '13 at 11:11
0

This piece of code needs to go after wp_insert_post. It basically just updates the post_author depending on the condition provided. Please note, that if the author is not logged in the post_author will default to admin again.

if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if ( 19 == $current_user->ID ) {
        $update_creator = array(
            'ID'           => $pid, // created post ID
            'post_author'  => '19'
        );

        wp_update_post( $update_creator );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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