This is pseudo-code answer.
- First get the posts you want with
get_posts
(you can pass the
post_type as an argument) - Then get the ID of the images you want to set as thumnbail
-
Now do a loop and try with a query similar to this one:
“UPDATE wp_postmeta SET meta_value = $image_id WHERE post_id = $post_id AND meta_key = ”_thumbnail_id””
NOTE: To get the $image_id from the array of key you could use array_rand
. You are gonna need wpdb
to run the query.
Read the docs, and ask a question if you have any doubts.