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

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.

share|improve this question
    
What are you trying to accomplish? –  Bainternet Oct 20 '11 at 23:10
    
Im currently working on a user search that will show users based on multiple meta keys. The theme Im using doesn't support a user search, so if a user doesn't have a post they wont be searchable on the site (which is really bad, I have 1500 users). So as a temporary solution I decide to create a post for every user so they would be searchable. –  user1893 Oct 21 '11 at 13:15
    
Well in that case this is a major overkill since you already have a users table and a user meta table so why not just create a function with a custom query? –  Bainternet Oct 21 '11 at 15:50
    
Yes, I have a working custom query that would be my solution to this problem. But there lies another problem... I want the users to be searched by category (description) keywords (which I found a fix, your solution [wordpress.stackexchange.com/questions/28407/…). This will fix the problem, but now I need to do a mass update for each user, who all have different categories. And this will take a long time to do individually. Can you think of a way to update each user's category selection on a mass scale? Thanks –  user1893 Oct 21 '11 at 15:58
    
Sure i can but based on what? –  Bainternet Oct 21 '11 at 17:17
show 2 more comments

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks so much! Im sure this will serve my purpose and open up more capabilities. –  user1893 Oct 20 '11 at 22:18
add comment

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.