Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Looping through array after shuffle() causes error
    primarykey
    data
    text
    <p>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.</p> <blockquote> <p>Warning: Invalid argument supplied for foreach()</p> </blockquote> <p>Here is where the error is happening</p> <pre><code>$tags = wp_get_post_terms( $post-&gt;ID , $taxonomy, $tax_args); $tags = shuffle($tags); if ($tags) { foreach ($tags as $tag) { // so on ... </code></pre> <p>and the full function</p> <pre><code>$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' =&gt; true), 'names' ); $tax_args=array('orderby' =&gt; 'none'); $tags = wp_get_post_terms( $post-&gt;ID , $taxonomy, $tax_args); $tags = shuffle($tags); if ($tags) { foreach ($tags as $tag) { $args=array( "$param_type" =&gt; $tag-&gt;slug, 'post__not_in' =&gt; array($post-&gt;ID), 'post_type' =&gt; $post_types, 'orderby' =&gt; 'rand', 'caller_get_posts' =&gt; 1 ); $my_query = null; $my_query = new WP_Query($args); if( $my_query-&gt;have_posts() ) { while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt; &lt;h3&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="&lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h3&gt; &lt;?php endwhile; } } } $post = $backup; // copy it back wp_reset_query(); // to use the original query again </code></pre> <p>Does anyone see anything wrong with that code? Any explanation much appreciated. Thanks!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload