I am obtaining the following error after shuffling an assortment, then attempting to loop through it. I am attempting to randomize an order of publish terms in the $tags variable.

Warning: Invalid argument provided for foreach()

Here's in which the error is going on

$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
$tags = shuffle($tags);
if ($tags) {
  foreach ($tags as $tag) { 
       // so on ...

and also the full function

$backup = $post; // backup the current object
$taxonomy = 'character' ;//  e.g. post_tag, category, custom taxonomy
$param_type = 'character'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
$tags = shuffle($tags);
if ($tags) {
  foreach ($tags as $tag) {
      "$param_type" => $tag->slug,
      'post__not_in' => array($post->ID),
      'post_type' => $post_types,
      'orderby' => 'rand',
      'caller_get_posts' => 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again

Does anybody see anything wrong with this code? Any explanation much appreciated. Thanks!!!

shuffle does not return the shuffled array, it rearranges the array in position. Apparently it returns boolean true or false on failure, but exactly how that may happen is really a mystery.

shuffle($array) returns a boolean: The case with FALSE. foreach needs an assortment, not really a boolean, which describes your error.

Simply write shuffle($array), not $array = shuffle($array)


bool shuffle ( array &$array )

That's correct: