Support » Fixing WordPress » Query All Posts in a Custom Post Type Outside Loop & Display by it's Term

  • Hello. I’ve searched the net thoroughly for this answer and though I see similar solutions, none are quite what I need bc if it fixes one thing it breaks another.

    So let’s say my custom post type is pets, one of my various taxonomies is dogs and one of my various terms is yorkies. I would like to show the last 5 added posts from my pets custom post type but I’d like them to be displayed by whatever term name they’re associated with & linked to their term archive(taxonomy.php). What I am hoping to end up with is a code like this in my footer(outside the loop):

    <h3>Most Recently Updated Categories</h3>
    <li><a href="PERMALINK-TO-TERM-ARCHIVE">YORKIES</a></li>
    <li><a href="PERMALINK-TO-TERM-ARCHIVE">CHIHUAHUAS</a></li>
    <li><a href="PERMALINK-TO-TERM-ARCHIVE">COLLIES</a></li>
    <li><a href="PERMALINK-TO-TERM-ARCHIVE">ROTTWEILERS</a></li>
    <li><a href="PERMALINK-TO-TERM-ARCHIVE">POMERANIANS</a></li>

    Basically every code I found wants me to specify a taxonomy & term name in the php code but seeing as how I want EVERY post in the pets custom post type to be queried no matter what taxonomy or term they have, I don’t think my code should specify the taxonomies & terms bc there are too many of them to list… Right?

    So could someone PLEASE write out the code from me based on this example?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve been trying to figure out how to do this for the past week and like you, I am completely stumped. Basically, I want to do a ‘related items’ loop below my single post loop on my single custom post type page templates that show a thumb and excerpt for each of maybe 3 or 4 items. This is kinda like on an ecommerce site when you are shopping one item and then they have a “Check out these other items” list.

    The ‘related items’ would be from the same term as the one being displayed in the single post loop on the page.

    This shouldn’t be this difficult, but as you mentioned—every. single. tutorial shows you how to display a loop if you know the parameters beforehand to put into the query, such as your term or taxonomy. Figuring out how to pull the term dynamically is another story and NOBODY has any tuts on how to do this, at least to my knowledge after searching for days on end and hacking my own approach and breaking my own page, repeatedly. I’m no PHP whiz, but I’ve been able to figure out most of my php woes until this one.

    I’m subscribing to this thread should someone come to the rescue. Likewise, if I figure this out I will let you know how I did it.

    I’ve tried getting the post ID, then getting the term of that post ID and a variety of other queries and I just can’t make it happen 🙁

    This is what I used, but it just returns the posts rather than my custom post types by term. Not sure why I can’t get it to work. It’s patterned after a function that was shared w/ me by Bill Erickson:

    function smt_related_items() {
    
     $term = get_the_terms( get_the_ID());
      $args = array(
        'posts_per_page' => 4,
        'post__not_in' => array( get_the_ID() ),
        'category_name' => $term[0]->slug
      );
      $loop = new WP_Query( $args );
    
      if( $loop->have_posts() ):
        echo '<div class="related-items"><h3>Related Items from Summit</h3>';
        while( $loop->have_posts() ): $loop->the_post();
          $classes = 'one-half relate-box';
          if( 0 == $loop->current_post || 0 == $loop->current_post % 2 )
            $classes .= ' first';
          echo '<div class="' . $classes . '"><a href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), 'pdt-thumb' ) . '</a><p>' . get_the_excerpt() .'</p></div>';
        endwhile;
        echo '</div>';
      endif;
      wp_reset_postdata();
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query All Posts in a Custom Post Type Outside Loop & Display by it's Term’ is closed to new replies.