Created
July 27, 2021 04:27
-
-
Save jasonbahl/03fb082e217f93af83310a6c09abc05c to your computer and use it in GitHub Desktop.
Create new posts with unique authors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'init', function() { | |
return; | |
$users = graphql([ | |
'query' => ' | |
{ | |
users(first:100) { | |
nodes { | |
databaseId | |
} | |
} | |
} | |
' | |
]); | |
$ids = isset( $users['data']['users']['nodes'] ) ? array_filter( array_map( static function( $user ) { | |
return $user['databaseId'] ?? null; | |
}, $users['data']['users']['nodes'])) : []; | |
foreach ( $ids as $user_id ) { | |
wp_insert_post([ | |
'post_type' => 'post', | |
'post_status' => 'publish', | |
'post_author' => $user_id, | |
'post_title' => 'Test July 26 - ' . $user_id, | |
'post_content' => 'test', | |
]); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For testing sake, I needed to create 100 new posts each with a different author.
Using GraphQL I was able to query for 100 users, loop over their databaseId, then pass that to wp_insert_post to create 100 new posts each with a different author.