0

I have a custom post type named artwork and I'm trying to get all of the artwork that is associated to the custom taxonomy of artists on the taxonomy-artists.php page.

I found an example here which works, but I'm wondering if I can trim it down a bit. I can't remember if I need to query the taxonomy term while on the taxonomy page. Seems like I don't need to if I am already on the specific page that has queried the custom taxonomy already. Here's the current code:

  <?php
$terms = wp_get_post_terms( $post->ID, 'artists' );
if($terms){
// post has course_type terms attached
$artists = array();
foreach ($terms as $term){
  $artists[] = $term->slug;
}

$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
  'post_type' => 'artwork',
  'tax_query' => array(
    array(
      'taxonomy' => 'artists',
      'field' => 'slug',
      'terms' => $artists, //the taxonomy terms I'd like to dynamically query
      'posts_per_page' => '-1'
    ),
  ),
  'orderby' => 'title',
  'order' => 'ASC'
) );

$image = get_field('artwork_image');
$size = 'artwork-small';
$img = $image['sizes'][ $size ];

if ( have_posts() ): ?>
<ul>
  <?php while (have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo $img ?>" alt="<?php echo $image['alt']; ?>" /><?php the_title(); ?></a></li>
  <?php endwhile; ?>
</ul>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
} // end if($terms)
?>

Like I said, it works, but just think there might be some extra stuff in there that I don't need if I'm already on the specific custom taxonomy page of taxonomy-artists.php

0

The link you added in your question is for Displaying the other post that share the same TERM.

And I guess you want to display the all post for the current taxonomy not the single term ?

Then just used the default $wp_query to do it just remove the custom wp_query and keep the loop same.

| improve this answer | |
  • Yeah, I just want to get all of the posts that are with the current taxonomy. So something more like this?: <?php $args = array( 'post_type' => 'artwork'); $loop = new WP_Query( $args ); ?> <?php while ( $loop->have_posts() ) : $loop->the_post();?> – ultraloveninja Apr 19 '15 at 19:47
  • No just use the direct loop <?php while(have_posts()) : the_post(); ?> – Sumit Apr 19 '15 at 19:59
  • Ahhhhh, ok. DUH. Yeah, that makes sense. Thanks for helping clear that up. – ultraloveninja Apr 20 '15 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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