Skip to content

Instantly share code, notes, and snippets.

@jasonbahl
Created July 27, 2021 04:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonbahl/03fb082e217f93af83310a6c09abc05c to your computer and use it in GitHub Desktop.
Save jasonbahl/03fb082e217f93af83310a6c09abc05c to your computer and use it in GitHub Desktop.
Create new posts with unique authors
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',
]);
}
} );
@jasonbahl
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment