I’m getting the following error after shuffling an array, then trying to loop through it. I’m trying to randomize the order of post terms from the $tags variable.
Warning: Invalid argument supplied for foreach()
Here is where the error is happening
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
$tags = shuffle($tags);
if ($tags) {
foreach ($tags as $tag) {
// so on ...
and 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) {
$args=array(
"$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>
<?php
endwhile;
}
}
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
Does anyone see anything wrong with that code? Any explanation much appreciated. Thanks!!!
shuffle($array)returns a boolean: TRUE of FALSE.foreachexpects an array, not a boolean, which explains your error.Simply write
shuffle($array), not$array = shuffle($array)