0

I'm creating a function that creates a page for a user when they register. The post created sits within a certain category. These are standard posts NOT custom post types.

After the post is created I want to grab the ID and overwrite the post_name with it.

I have this so far, create the post:

function add_memberzone_page( $user_id ) {

    global $wpdb;

    $user = get_userdata( $user_id );

    $where = get_posts_by_author_sql( 'post', true, $user_id );
    if( $wpdb->get_var( 'SELECT COUNT(*) FROM $wpdb->posts $where' ) >= 1 ) return;

    $user_page = array(
        'post_title' => $user->first_name,
        'post_type' => 'post',
        'post_content' => '',
        'post_excerpt' => '',
        'post_category' => '21',
        'post_author' => $user_id,
        'post_status' => 'publish',
    );

    wp_insert_post( $user_page );
}
add_action( 'user_register', 'add_memberzone_page', 999 );

update the post:

function update_memberzone_page( $post_id ) {

    global $wpdb;

    $post_id = wp_insert_post( $user_page );

    $post_update = array(
        'post_name' => $post_id,
    );

    update_post_meta( $post_update );
}
add_action( 'save_post', 'update_memberzone_page', 10, 3 );

Any help with pushing me in the right direction is much appreciated!

0

No need for the two functions/hooks - just use the first, with the addition of:

if ( $post_id = wp_insert_post( $user_page ) ) {
    wp_update_post(
        array(
            'ID'        => $post_id,
            'post_name' => $post_id,
        )
    );
}
0

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.