Create post for every user?

I was wondering is there a way to create a post for every user. Sort of like a mass update that would associate certain user meta to the post created.

I would prefer running a loop script that goes through all users and creates a post for each using certain user meta info.

Read More

For instance, if I have five users that I would like to create posts for. The posts would be titled using each of their full names.

Basic Example:

Users

johnny

jannie

bobbie

craig

mitch

User Meta

John Doe

Jane Doe

Bob Collins

Craig Matthews

Mitchell Humphries

Post Titles:

John Doe

Jane Doe

Bob Collins

Craig Matthews

Mitchell Humphries

This request is uncommon but I need a temporary fix until I finish a plugin for full user search. Any help with this would be greatly appreciated.

Related posts

Leave a Reply

1 comment

  1. Create this as a plugin and activate it. It deactivates itself after it creates a primary page for Authors and sub-pages for each Author+ or better Role. At the end, it deactivates itself.

    Improve it as you wish. It uses

    // Hook activation to create new Author Pages
    register_activation_hook(__FILE__, function(){
        // Create a Parent Page for all Author Pages
        if(!($parent = get_page_by_title('Authors'))){
            $parent = wp_insert_post(array(
                'post_type' => 'page',
                'post_title' => 'Authors',
                'post_content' => 'Authors are children of this page.',
                'post_status' => 'draft', // Or publish
            ));
        }
        if(!$parent){
            // Bad... ERROR!
            return;
        }
        // Get user IDs, get_users() returns too much data
        global $wpdb;
        $IDs = $wpdb->get_col("SELECT `ID` FROM {$wpdb->users} ORDER BY `user_registered` DESC;");
        // Loop IDs and create subpages for Authors+ (not Subscribers)
        foreach($IDs as $ID){
            // Get user
            $user = new WP_User($ID);
            // Only create pages for Authors!
            if(!$user->has_cap('edit_posts')) continue;
            // Create page for Author
            $title = "About Author: {$user->display_name}";
            if(!($child = get_page_by_title($title))){
                $child = wp_insert_post(array(
                    'post_type' => 'page',
                    'post_title' => $title,
                    'post_name' => $user->display_name,
                    'post_content' => 'Write stuff about the Author.',
                    'post_status' => 'draft', // Or publish
                    'post_parent' => $parent,
                ));
                // Setup according Metas (for further tracking)
                update_post_meta($child, 'about_author', $user->user_login);
                update_post_meta($child, 'about_author_ID', $user->ID);
            }
        }
        // Done! WILL RUN JUST ONCE, deactivates itself afterwards.
        deactivate_plugins(__FILE__, true);
        die;
    });
    

    It’s kind of a hacky approach but will do what you need. It uses a PHP 5.3 Closure. Consider reverting to PHP 5.2 compatibility an assignment 🙂

    Regards.