I am trying to create a list of tags that I can use in an array within a wordpress wp_query. I’ve made the tags appear in the template (via echo) so I know I have the output, but I don’t know how to move/use the output from this into the array of the separate wp_query. I normally can find out how to do things via searching but I don’t know what the name is of what I am trying to do. I’m self/internet taught.
$commatext = ","; $blankcat = "blankcategory"; $tags = wp_get_post_tags( $post->ID ); if ( !empty( $tags ) && !is_wp_error( $tags ) ): foreach ( $tags as $tag ): if(strpos($tag->slug,'sim-') !== false): echo $tag->slug; echo $commatext; endif; endforeach; echo $blankcat; endif;
This outputs:
sim-simname1,sim-simname2,blankcategory
I need to figure out how to put that into a wp_query for the same post:
'tag' => array( sim-simname1,sim-simname2,blankcategory )
I do have it working right now but I have to manually type 256 possible tags with sim-… slugs into my wp_query. Automating that would be nice…
Advertisement
Answer
Build a list of your collected sims in a plain array and pass that in an associative array (map) to WP_Query.
Untested but something like …
if ( !empty( $tags ) && !is_wp_error( $tags ) ): $simarr = array(); foreach ( $tags as $tag ): if(strpos($tag->slug,'sim-') !== false): $simarr[] = $tag->slug; // push 'sim-' into array endif; endforeach; $simarr[] = "blankcategory"; // add to end of array $query = new WP_Query( array('tag' => $simarr) ); endif;
Hope that’s close.
Dave