Created
November 24, 2016 21:57
-
-
Save akkyie/386090f7d38706b5bde28756f500af2a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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