Having some trouble with a custom taxonomy archive template (3 posts)

  1. robotor
    Posted 2 years ago #

    Hello all, I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.

    Goal example: to have an archive template that shows only posts with a certain taxonomy term in it at <domain>/people/harrison-ford

    1) This code is bringing in posts that do not have the taxonomy selected.

    2) This is not returning the name for some reason: <?php echo $tax_term->name; ?> News</h2>

    Here's my code:

    	$year = get_post_meta($post->ID, 'year', true);
    	$post_type = 'post';
    	$tax = 'people';
    	$tax_terms = get_terms( $tax );
    	if ($tax_terms) {
    		$args = array(
    			'post_type' => $post_type,
    			'people' => 'harrison-ford',
    			"$tax" => $tax_term->slug,
    			'post_status' => 'publish',
    			'posts_per_page' => -1,
    			'caller_get_posts'=> 1,
    				'orderby' => 'date',
    				'order' => DESC
    		$my_query = null;
    		$my_query = new WP_Query($args);
    		if( $my_query->have_posts() ) : ?>
    			<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
    			<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <-- display stuff -->
    			<?php endwhile; // end of loop ?>
    		<?php endif; // if have_posts()
  2. davidsword
    Posted 2 years ago #

    you're calling $tax_term->slug and $tax_term->name but haven't a $tax_term object, you have $tax_terms with an S, but not one without.

    I think you meant to do something like:

    $this_term = get_term_by('slug',$wp->query_vars['people'],'people');
    echo "<h2>{$this_term->name}</h2>";
    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    			'taxonomy' => 'people',
    			'field' => 'slug',
    			'terms' => $this_term->slug // i.e. 'harrison-ford'
    $query = new WP_Query( $args );

    I didn't test it, but try reading a bit more on the power of WP_Query Tax Peramatars http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters and http://codex.wordpress.org/Function_Reference/get_term_by

  3. robotor
    Posted 2 years ago #

    Thanks - will check this out and do some testing!

Topic Closed

This topic has been closed to new replies.

About this Topic