Skip to content

Instantly share code, notes, and snippets.

@akkyie
Created November 24, 2016 21:57
Show Gist options
  • Save akkyie/386090f7d38706b5bde28756f500af2a to your computer and use it in GitHub Desktop.
Save akkyie/386090f7d38706b5bde28756f500af2a to your computer and use it in GitHub Desktop.
<?php
$posts_query = new WP_Query(['posts_per_page' => -1]);
$images_query = new WP_Query([
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit',
'posts_per_page' => -1,
]);
$images = $images_query->posts;
while ($posts_query->have_posts()) {
$posts_query->the_post();
$tags = get_the_tags();
if ($tags == false) { continue; }
foreach ($tags as $tag) {
$path = parse_url($tag->name, PHP_URL_PATH);
$matches = array_filter($images, function ($image) use ($path) {
return strpos($path, parse_url($image->guid, PHP_URL_PATH)) !== false;
});
if (count($matches) < 1) { continue; }
$match = array_shift($matches);
set_post_thumbnail(get_the_ID(), $match->ID);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment