Working with get_posts()

get_posts() is a simple WordPress function that allows you to retrieve a list of posts that match certain criteria, i.e. post type, post status, or posts “tagged” by a certain taxonomy.

As of version 1.0.2, CPT-onomies works with get_posts() taxonomy queries BUT you have to set the ‘suppress_filters’ argument to false.

Otherwise, WordPress default-ly defines ‘suppress_filters’ as true which disables all the filters that allow CPT-onomies to work. See below for an example.

FYI: If using the tax query, and querying by ID, a CPT-onomy term’s term ID is the same as it’s post ID.

Display ‘movies’ that include ‘Tom Hanks’ from the ‘actors’ CPT-onomy:

The first example uses a quick ‘taxonomy’ => ‘term_slug’ argument.

$movies = get_posts( array(
   'numberposts' => -1, // we want to retrieve all of the posts
   'post_type' => 'movies',
   'actors' => 'tom-hanks', // we want posts who are tagged with the actors taxonomy term with the slug 'tom-hanks'
   'suppress_filters' => false // this argument is required for CPT-onomies
) );

if( $movies ) {
   ?><h3>Movies</h3>
   <ul><?php
      foreach ( $movies as $movie ) {
         ?><li><a href="<?php echo get_permalink( $movie->ID ) ?>" title="<?php echo get_the_title( $movie->ID ); ?>"><?php echo get_the_title( $movie->ID ); ?></a></li><?php
      }
   ?></ul><?php
}

get_posts() also lets you get a little more finnicky while defining your taxonomy query:

$movies = get_posts( array(
   'numberposts' => -1, // we want to retrieve all of the posts
   'post_type' => 'movies',
   'suppress_filters' => false, // this argument is required for CPT-onomies
   'tax_query' => array(
      array(
         'taxonomy' => 'actors',
	 'field' => 'id', // can be slug or id - a CPT-onomy term's ID is the same as its post ID
	 'terms' => 303
      )
   )
) );

if( $movies ) {
   ?><h3>Movies</h3>
   <ul><?php
      foreach ( $movies as $movie ) {
         ?><li><a href="<?php echo get_permalink( $movie->ID ) ?>" title="<?php echo get_the_title( $movie->ID ); ?>"><?php echo get_the_title( $movie->ID ); ?></a></li><?php
      }
   ?></ul><?php
}

WordPress Codex

For more information, refer to the WordPress codex.