Last active
April 10, 2023 16:03
-
-
Save Tusko/e7bdba33303404e7b46177fb99a92cde to your computer and use it in GitHub Desktop.
Append custom taxonomy to CPT
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
<?php | |
$tax = 'YOUR_TAX_HERE'; | |
$cpt = 'YOUR_CPT_HERE'; | |
$count = 3; | |
$types = get_terms(array( | |
'taxonomy' => $tax, | |
'hide_empty' => false, | |
'parent' => 0 | |
)); | |
$arr = array( | |
'post_type' => $cpt, | |
'posts_per_page' => -1 | |
); | |
$func = function($value) { | |
return $value->term_id; | |
}; | |
$ids = array_map($func, $types); | |
$q = new WP_Query($arr); | |
while($q->have_posts()) : $q->the_post(); | |
global $post; | |
$randIndex = array_rand($ids, 3); | |
$randomTax = array(); | |
foreach($randIndex as $i) { | |
$randomTax[] = $ids[$i]; | |
} | |
wp_set_post_terms($post->ID, $randomTax, $tax, false ); | |
endwhile; | |
wp_reset_query(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment