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 a custom tag-like taxonomy called PEOPLE registered to all post types - both the default and custom ones.

I have created a template for a single term named taxonomy-people.php and I'm intending to use it to describe a specific term and query each post type for instances of that term.

I have almost managed to do that, there's a single issue left: I don't know how to get WP_Query to show only results for the current term WITHOUT having to make a specific template for each of the terms, as I will end up having a three-digit number of terms under PEOPLE.

Here's the code that works if I specify a term. What should I replace 'people' => 'dave-lee' with in order to use the same template for each of the terms under PEOPLE?

<?php get_header(); ?>

<div class="eight columns" id="content">

<div class="post">

<div class="inside">

<p class="term-description">
<?php $termDescription = term_description( '', get_query_var( 'taxonomy' ) ); if($termDescription != '') : ?> <?php echo $termDescription; ?> <?php endif; ?>
</p>


<h3>News mentioning <?php $term =   $wp_query->queried_object; echo ''.$term->name.'';?></h3>

<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'post', 'people' => 'dave-lee', 'posts_per_page' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?>      </a><br />
<?php _e('Published on','jf'); ?> <?php the_time(get_option('date_format')); ?></li>
<?php endwhile; ?>
</ul>

<?php wp_reset_query(); ?>

<h3>Releases <?php $term =  $wp_query->queried_object; echo ''.$term->name.'';?> contributed to:</h3>

<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'release', 'people' => 'dave-lee', 'posts_per_page' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?>    </a><br />
<?php _e('Published on','jf'); ?> <?php the_time(get_option('date_format')); ?></li>
<?php endwhile;?>
</ul>

 <?php wp_reset_query(); ?>

 <h3>Songs featuring <?php $term =  $wp_query->queried_object; echo ''.$term->name.'';?>:</h3>

<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'song', 'people' => 'dave-lee', 'posts_per_page' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php _e('Published on','jf'); ?> <?php the_time(get_option('date_format')); ?></li>
<?php endwhile;?>
</ul>

<?php wp_reset_query(); ?>

<h3>Articles, reviews and other pages mentioning <?php $term =  $wp_query->queried_object; echo ''.$term->name.'';?>:</h3>

<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'page', 'people' => 'dave-lee',     'posts_per_page' => 20 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="archivepostlink"><a href="<?php the_permalink(); ?>"><?php the_title(); ?>    </a><br />
<?php _e('Published on','jf'); ?> <?php the_time(get_option('date_format')); ?></li>
<?php endwhile;?>
</ul>

<?php wp_reset_query(); ?>

<p>If you think <?php $term =   $wp_query->queried_object; echo ''.$term->name.'';?> was/were a part of something else that has not been listed here, drop a line.</p>
 </div>
 </div>
 </div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>

Thank you.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Your code, when working, will consume sooo much resources. When implemented in this way - throught custom page template, WordPress will call for a content of that page (1 query) and after that it will call a query for each custom post type - another 4 queries. That's a lot, really.

I would suggest you to take the advantage of pre_get_posts hook, where you can alter the query before any query is run. There you can specify what post types shold be queried. You can use something like this:

function extend_post_type( $query ) {
    if ( $query->is_archive() && isset($query->query_vars['people']) && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'release', 'song', 'page') );
        $query->set( 'posts_per_page', -1 );
    }
}
add_action( 'pre_get_posts', 'extend_post_type' );

In this way, you'll get the query full of post_types from people taxonomy. Now it's time to loop the whole query and sortout the content by post type.

I would use something like that:

if ( have_posts() ) {
    while( have_posts() ) { the_post();
        if ( get_post_type() == 'post' ){
                $posts_html .= '<a href="'.get_permalink().'" title="'.the_title_attribute( 'echo=0' ).'">' . get_the_title() . '</a>';
        }elseif( get_post_type() == 'page' ){
                $pages_html .= '<a href="'.get_permalink().'" title="'.the_title_attribute( 'echo=0' ).'">' . get_the_title() . '</a>';
        }elseif( get_post_type() == 'song' ){
                $songs_html .= '<a href="'.get_permalink().'" title="'.the_title_attribute( 'echo=0' ).'">' . get_the_title() . '</a>';
        }elseif( get_post_type() == 'release' ){
                $releases_html .= '<a href="'.get_permalink().'" title="'.the_title_attribute( 'echo=0' ).'">' . get_the_title() . '</a>';
        }
    }
    $term = get_term_by('slug', get_query_var( 'people' ), 'people' );
    echo '<h2>News for "'.$term->name.'"';
    echo $posts_html;
    echo '<h2>Releases for "'.$term->name.'"';
    echo $releases_html;
    echo '<h2>Songs for "'.$term->name.'"';
    echo $songs_html;
    echo '<h2>Pages for "'.$term->name.'"';
    echo $pages_html;
}

This approach uses only one query request and thus is resources friendly.

share|improve this answer
    
Thank you for your kindness, but it seems that I am not 100% sure what I am doing or I am missing something fairly obvious. I have added the first snippet to functions.php and the later to taxonomy-people.php and that crashed the entire website. I apologise if I'm being thick - I have not used this hook before. –  Iva Feb 11 '13 at 8:24
    
There were some typos in my code - feel free to grab it again ;). For the case your entire page goes down (not just this time, ever) - try to set WP_DEBUG true in you wp-config.php ( see codex.wordpress.org/WP_DEBUG ) –  david.binda Feb 11 '13 at 8:42
    
Now I'm getting entire posts, but the basics are there - thank you. I guess I'll modify $posts_html, $pages_html and the other two to only show the titles and permalinks and that should be it. Thank you so much! :) –  Iva Feb 11 '13 at 8:47
    
You're right - modify those variables in a way you need... –  david.binda Feb 11 '13 at 8:49
    
I am not sure how to output the permalink instead of h3 and how to get The Name instead of the-name to show. –  Iva Feb 11 '13 at 10:04

If you have taxonomy people, and people like Bobby Lee, Michael Jordan, Matt Mullenweg.

whenever someone goes to the page: http://domain.tld/people/bobby-lee, he will get all the posts that are in that taxonomy. No matter the post type.

Iva said:

I don't know how to get WP_Query to show only results for the current term WITHOUT having to make a specific template for each of the terms

If you go to: http://etfovac.com/tehnologije/torenti/ You will see that taxonomy tehnologije spans two post types (tutorials and wordpress) and that both are being displayed for the term torenti in the tehnologije taxonomy.

Therefore, my response is: Do not mess with WP_Query. Default Query will pull all the posts that contain specific term.

share|improve this answer

Don't use WP_Query, use query_posts instead to modify the query.

WP_Query overrides everything.

share|improve this answer
    
@SeeBeen I think you have your logic mixed up. If you want to make new queries you should always use new WP_Query not query_posts() –  Brady Feb 11 '13 at 9:43
    
The point is that she shouldn't use new Query –  SeeBeen Feb 12 '13 at 7:31

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.