You are viewing an old revision of this post, from July 2, 2013 @ 12:12:26. See below for differences between this version and the current revision.

WordPress: combine two queries into one loop

[php] <?php $term = get_term_by( 'slug', get_query_var( 'tag' ), "post_tag" ); $tagslug = $term->slug; $post_types = get_post_types('','names'); ?> <?php //first query $blogposts = get_posts(array( 'tag' => $tagslug, //first taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); //second query $authorposts = get_posts(array( 'bookauthor' => $tagslug, //second taxonomy 'post_type' => $post_types, 'post_status' => 'publish', )); $mergedposts = array_merge( $blogposts, $authorposts ); //combine queries $postids = array(); foreach( $mergedposts as $item ) { $postids[]=$item->ID; //create a new query only of the post ids } $uniqueposts = array_unique($postids); //remove duplicate post ids $posts = get_posts(array( 'post__in' => $uniqueposts, //new query of only the unique post ids on the merged queries from above 'post_type' => $post_types, 'post_status' => 'publish', )); foreach( $posts as $post ) : setup_postdata($post); ?> [/php]

Revisions

Revision Differences

July 2, 2013 @ 12:12:26Current Revision
Content
Unchanged: [php]Unchanged: [php]
Deleted: &lt;pre&gt;&lt; code&gt;&lt;?phpAdded: &lt;?php
Unchanged: $term = get_term_by( 'slug', get_query_var( 'tag' ), &quot;post_tag&quot; );Unchanged: $term = get_term_by( 'slug', get_query_var( 'tag' ), &quot;post_tag&quot; );
Unchanged: $tagslug = $term-&gt;slug;Unchanged: $tagslug = $term-&gt;slug;
Unchanged: $post_types = get_post_types( '','names');Unchanged: $post_types = get_post_types( '','names');
Unchanged: ?&gt;Unchanged: ?&gt;
Unchanged: &lt;?phpUnchanged: &lt;?php
Unchanged: //first queryUnchanged: //first query
Unchanged: $blogposts = get_posts(array(Unchanged: $blogposts = get_posts(array(
Unchanged: 'tag' =&gt; $tagslug, //first taxonomyUnchanged: 'tag' =&gt; $tagslug, //first taxonomy
Unchanged: 'post_type' =&gt; $post_types,Unchanged: 'post_type' =&gt; $post_types,
Unchanged: 'post_status' =&gt; 'publish',Unchanged: 'post_status' =&gt; 'publish',
Unchanged: ));Unchanged: ));
Unchanged: //second queryUnchanged: //second query
Unchanged: $authorposts = get_posts(array(Unchanged: $authorposts = get_posts(array(
Unchanged: 'bookauthor' =&gt; $tagslug, //second taxonomyUnchanged: 'bookauthor' =&gt; $tagslug, //second taxonomy
Unchanged: 'post_type' =&gt; $post_types,Unchanged: 'post_type' =&gt; $post_types,
Unchanged: 'post_status' =&gt; 'publish',Unchanged: 'post_status' =&gt; 'publish',
Unchanged: ));Unchanged: ));
Unchanged: $mergedposts = array_merge( $blogposts, $authorposts ); //combine queriesUnchanged: $mergedposts = array_merge( $blogposts, $authorposts ); //combine queries
Unchanged: $postids = array();Unchanged: $postids = array();
Unchanged: foreach( $mergedposts as $item ) {Unchanged: foreach( $mergedposts as $item ) {
Unchanged: $postids[]=$item-&gt;ID; //create a new query only of the post idsUnchanged: $postids[]=$item-&gt;ID; //create a new query only of the post ids
Unchanged: }Unchanged: }
Unchanged: $uniqueposts = array_unique($postids); //remove duplicate post idsUnchanged: $uniqueposts = array_unique($postids); //remove duplicate post ids
Unchanged: $posts = get_posts(array(Unchanged: $posts = get_posts(array(
Unchanged: 'post__in' =&gt; $uniqueposts, //new query of only the unique post ids on the merged queries from aboveUnchanged: 'post__in' =&gt; $uniqueposts, //new query of only the unique post ids on the merged queries from above
Unchanged: 'post_type' =&gt; $post_types,Unchanged: 'post_type' =&gt; $post_types,
Unchanged: 'post_status' =&gt; 'publish',Unchanged: 'post_status' =&gt; 'publish',
Unchanged: ));Unchanged: ));
Unchanged: foreach( $posts as $post ) :Unchanged: foreach( $posts as $post ) :
Unchanged: setup_postdata($post);Unchanged: setup_postdata($post);
Deleted: ?&gt;&lt;/code&gt; &lt;/pre&gt;Added: ?&gt;
Unchanged: [/php]Unchanged: [/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: , ,

No comments yet.

Leave a Reply