Wp Query for custom post type’s taxonomy

Wp Query for custom post type’s taxonomy

To retrieve posts from a WordPress CMS, it uses a loop and queries. And you can customize your queries to show only those you want to show. When using custom post types and taxonomies it gets trickier. Here’s how.

The Loop – reminder

To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show. If there are, a while loop is begun, using have_posts() as the condition. This will iterate around as long as there are posts to show. In each iteration, the_post(), which calls $wp_query->the_post() is called, setting up internal variables within $wp_query and the global $post variable (which the Template Tags rely on).

The structure looks like this:

if ( have_posts() ) { 
    while ( have_posts() ) { 
        the_post(); 
            // Post Content here 
    } // end while 
} else { 
    // no posts to show 
}// end if

Or, on a PHP twist:

if ( have_posts() ) : while ( have_posts() ) : the_post();
    // Post Content here
endwhile; else:
    // no posts to show
endif;

If you are to design your own query, there are two ways of doing it.

1. Modifying the original loop

This query actually modifies the existing loop query by adding arguments and it is necessary to restore to defaults later.It looks like this:

//The Query
query_posts($args);
//The Loop
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		// Post Content here
	} // end while
} else {
        // no posts to show
}// end if
// Reset
wp_reset_query();

query_posts() is meant for altering the main loop. It does so by replacing the query used to generate the main loop content. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered – this may or may not be the intended result.

2. Using WP-Query

For multiple loops within a same page, it is better to use this other option. WP-Query creates a query that is stored in a variable of your choice, not affecting the default loop workings.

// The Query
$your_query = new WP_Query( $args );
// The Loop
if ( $your_query->have_posts() ) {
	while ( $your_query->have_posts() ) {
		$your_query->the_post();
		echo '<ul> <li>' . get_the_title() . '</li> </ul>';
	}
} else {
	// no posts found
}
// Reset
wp_reset_postdata();

To create multiple loops unaffecting one another simple use a new variable instead of $your_query.

Arguments

There is a lot you can do by combining different argument parameters, like filtering in/out categories, post-types, authors…
Here’s one thing I’ve had to check up several times before. How to retrieve posts from a custom post type and filter in/out one of its taxonomy values. For more info check: WP-Query Codex entry and query_posts Codex entry.

Say you have a custom post type named Portfolio Items (with slug portfolio-items). Say there is a taxonomy called Portfolio Categories (with slug portfolio-categories). And finally say that you would like to exclude one of them called Excluded Portfolio Category (with slug ex-portfolio-cat). Here’s the tax query that you would pass on as arguments in $args:

// The arguments
$args=array(
	'post_type' => portfolio-items,
	'tax_query' => array(
		array(
			'taxonomy' => 'portfolio-categories',
			'field' => 'slug',
			'terms' => 'ex-portfolio-cat',
			'operator' => 'NOT IN'
		)
));
// The Query
$your_query = new WP_Query( $args );
// The Loop
if ( $your_query->have_posts() ) {
	while ( $your_query->have_posts() ) {
		$your_query->the_post();
		echo '<ul> <li>' . get_the_title() . '</li> </ul>';
	}
} else {
	// no posts found
}
// Reset
wp_reset_postdata();

What about ONLY querying for one value of the taxonomy? Here’s an example:

// The arguments
$args=array(
  'post_type' => 'portfolio-items',
  'portfolio-categories' => 'ex-portfolio-cat',
  );
//The Query
query_posts($args);
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
    // Post Content here
endwhile; else:
    // no posts to show
endif;
// Reset
wp_reset_postdata();

Query Generator

I found this online WordPress Query Generator. It only works for standard posts, though, not custom post types. But it’s still worth a share.

Steven Tyler

Next Article

Steven Tyler

One Comment

Cancel

  1. Spijker op z’n kop: de reis is het doel! Het maakt dan ook niet uit waar je uiteindelijk bent, genieten kan overal. Het zijn de mensen waarmee je de reis maakt en die je ontmoet die het de moeite waard maken. Kortom roer om en ‘op reis’ naar de nieuwe bengmemist.Grtz, Geert-Janps kan me absoluut niet voorstellen hoe Ido met opgemaakte rode lippen er uit ziet ronddrijvend in een Frans zwembad