Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active April 10, 2023 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tusko/e7bdba33303404e7b46177fb99a92cde to your computer and use it in GitHub Desktop.
Save Tusko/e7bdba33303404e7b46177fb99a92cde to your computer and use it in GitHub Desktop.
Append custom taxonomy to CPT
<?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