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 have 2 customs post type : "Artist" and "Concert",
  • the "Concert" custom post type is the child of the "Artist" custom post type,
  • the "Artist" custom post type has a "genre" taxonomy.

What I'm trying to do (for instance): list all the concerts which belong to artists of the "pop" genre.

Here is the query of my dream:

SELECT * FROM posts WHERE post_type = "concert" AND post_parent_term = "pop"

I think currently there is no such thing as post_parent_term, hope I'm wrong ... (I know i can add the "genre" taxonomy to the "Concert" custom post type and voilà! But I'm really curious to know if there is another way to achieve that).

Thanks by advance.

share|improve this question
    
You don't get the posts in a taxonomy; you get the posts associated to terms in a given taxonomy. In your example: term = 'pop'; taxonomy = 'genre'. –  scribu Mar 14 '11 at 0:22
add comment

3 Answers

up vote 3 down vote accepted

What I'm trying to do (for instance): list all the concerts which belong to artists of the "pop" genre.

You can do it in two steps:

// 1. Get all the pop artist IDs
$artist_ids = get_posts( array(
  'fields' => 'ids',
  'post_type' => 'artist',
  'genre' => 'pop'
) );

// 2. Get all the concerts associated to those artists
$artist_ids = implode( ',', array_map( 'absint', $artist_ids ) );

$concerts = $wpdb->get_results( "
  SELECT * FROM $wpdb->posts
  WHERE post_type = 'concert'
  AND post_status = 'publish'
  AND post_parent IN ({$artist_ids})
  ORDER BY post_date DESC
" );

There's a post_parent argument in WP_Query, but it doesn't accept an array, hence the direct query.

share|improve this answer
    
now that i see this I'm thinking to my self "how dumb can i be?", Nice one +1. –  Bainternet Mar 14 '11 at 0:50
    
Thank you very much for your help, it's working perfectly ! –  inwpitrust Mar 25 '11 at 22:31
add comment

The parent page is stored in $post->post_parent

So you can just grab the parent post that way, and then ask it for it's taxonomy/category/tag info.

share|improve this answer
add comment

Not sure if its the right way but you could create nested loops:

//first get all artists with the term pop

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'slug',
            'terms' => 'pop'
        ))
    'post_type' => 'Artist',
    'posts_per_page' => -1
    );
$Artists = new WP_Query( $args );
//loop through them and get there child posts of concerts 
if ( $Artists->have_posts() ) { 
    while ( $Artists->have_posts() ) {
        $Artists->the_post();
        $last_artist = $post;
        $Concerts = new WP_Query();
        $Concerts->query(array(
                        'post_type' => 'concert',
                        'posts_per_page' => -1,
                        'post_parent' => $post->ID
                        ));
        while ( $Concerts->have_posts() ) {
            $Concerts->the_post();
            //do concert stuff here
            //the_title();
            //the_content();
        }
        wp_reset_postdata();
        $post = $last_artist;
    }
}
share|improve this answer
1  
Yeah, nested WP_Query()s are a bad idea. –  scribu Mar 14 '11 at 0:52
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.