Created
October 13, 2012 19:18
-
-
Save chrisdigital/3885821 to your computer and use it in GitHub Desktop.
How to do a custom wp_query in wordpress with a custom post type , taxonomy and tags
This file contains 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 | |
<?php | |
$args1 = array( | |
//choose what post type you want to pull from | |
'post_type' => array( 'news' ), | |
//speed up query by only grabbing published items | |
'post_status' => 'publish', | |
//drill down into postype with a specific taxonomy via a tax_query | |
'tax_query' => array( | |
//use 'OR' to be additive (e.g. either this or this) or 'AND' to filter for specifics (e.g. must be this and this) | |
//filtering on the slug field of each peice of data | |
'relation' => 'OR', | |
array(//filtering on category | |
'taxonomy' => 'category', | |
'field' => 'slug', | |
'terms' => array( 'media coverage' ) | |
), | |
array(//filtering on tags | |
'taxonomy' => 'post_tag', | |
'field' => 'slug', | |
'terms' => array( 'featured-our-research' ) | |
) | |
) | |
); | |
$articles_query = new WP_Query( $args1 ); | |
if ( $articles_query->have_posts() ) { | |
while ( $articles_query->have_posts() ) { | |
$articles_query->the_post(); | |
/// do stuff here | |
echo'<li><a href="'; the_permalink(); echo'" rel="bookmark">'; the_title(); echo'</a></li>'; | |
} | |
} | |
?> ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment