Created
March 7, 2014 13:17
-
-
Save illucent/9411300 to your computer and use it in GitHub Desktop.
wp loop examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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( | |
| //new query of only the unique post ids on the merged queries from above | |
| 'post__in' => $uniqueposts, | |
| 'post_type' => $post_types, | |
| 'post_status' => 'publish', | |
| )); | |
| foreach( $posts as $post ) : | |
| setup_postdata($post); | |
| ?> | |
| // posts layout | |
| <?php endforeach; ?> | |
| <?php wp_reset_postdata();?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment