Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to display all the posts that use a custom taxonomy. I believe the issue is that it is my code is not generating an array of terms.

 $clients_terms = get_terms('clients');

    $clients_query = new WP_Query( array(
      'post_type' => 'work',
      'posts_per_page' => 4,
      'tax_query' => array(
        array(
          'taxonomy' => 'clients',
          'field' => 'slug',
          'terms' => array($clients_terms->slug)
        )
      )
    ) );
    if ( $clients_query->have_posts() ): ?>
    <ul class="work_list">
      <?php while ( $clients_query->have_posts() ) : $clients_querys->the_post(); ?>
      <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>   </li>
      <?php endwhile; wp_reset_postdata(); ?>
    </ul>

I'm able to generate posts for individual terms but not for all posts that use the taxonomy .

share|improve this question
1  
What is the output of array($clients_terms->slug)? That doesn't seem correct syntax to me. get_terms() returns an array of objects; not an object. –  Chip Bennett Oct 23 '13 at 12:32
add comment

2 Answers

up vote 2 down vote accepted

I think your problem is here:

'terms' => array($clients_terms->slug)

You define $clients_terms like so:

$clients_terms = get_terms('clients');

...which will return an array of objects. But you try to access it as an object in your query args. That should be producing some kind of PHP notice? (Trying to get property of non-object, or something?)

Try building the array, then passing it to 'terms':

// Get the array of objects
$clients_terms_objs = get_terms( 'clients' );

// Define terms array
$clients_terms = array();

// Step through array of objects,
// and populate terms array
foreach ( $clients_terms_objs as $clients_term_obj ) {
    $clients_terms[] = $clients_term_obj->slug;
}

// Construct query
$clients_query = new WP_Query( array(
  'post_type' => 'work',
  'posts_per_page' => 4,
  'tax_query' => array(
       array(
          'taxonomy' => 'clients',
          'field' => 'slug',
          'terms' => $clients_terms
       )
    )
) );
share|improve this answer
    
Thanks that seems to have resolved the issue but I wasn't getting any php notice? Thanks again –  Josh Oct 23 '13 at 13:47
    
Do you have WP_DEBUG enabled? –  Chip Bennett Oct 23 '13 at 13:49
add comment

Here is an alternate solution using filters that doesn't require the get_terms() call.

/**
 * Extends WP_Query with a posts_join filter allowing you to query by taxonomy instead of tax_query using terms
 * Usage: <code> $query = new Query_By_Taxonomy( array( 'posts_per_page' => $foo, 'orderby' => $bar ) ); </code>
 *
 * @class Query_By_Taxonomy
 */
class Query_By_Taxonomy extends WP_Query {

    var $posts_by_taxonomy;
    var $taxonomy;

    function __construct( $args = array() ) {
        add_filter( 'posts_join', array( $this, 'posts_join' ), 10, 2 );
        $this->posts_by_taxonomy = true;
        $this->taxonomy = $args['taxonomy'];

        unset( $args['taxonomy'] );

        parent::query($args);
    }

    function posts_join( $join, $query ) {
        if ( isset( $query->posts_by_taxonomy ) && false !== $query->posts_by_taxonomy ) {
            global $wpdb;
            $join .= $wpdb->prepare(
                 "INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
                  INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
                  AND {$wpdb->term_taxonomy}.taxonomy=%s",
                $this->taxonomy );
        }
        return $join;
    }
}

Usage:

$clients_query = new Query_By_Taxonomy(
    array(
      'post_type' => 'work',
      'posts_per_page' => 4,
      'taxonomy' => 'clients',
  )
);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.