WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Multiple Custom Post Type taxonomy terms loops - how? (7 posts)

  1. flint_and_tinder
    Member
    Posted 1 year ago #

    Hi all,

    I've set up a CPT called 'fan' and made a taxonomy for it called 'fan_post_types'. Within that taxonomy I have two terms called 'fan-comment' and 'fan-merchandise'.

    What I am trying to do now is set up a page called 'fans' and include 2 loops within it, one for the posts of both taxonomy terms so I have a feed of each.

    I'm using WP_query loops at the moment, but I can't get them to work. Both loops show posts from both taxonomy terms, instead of one loop showing one set and the other loops displaying the rest. Can anybody help please?

    Here is my current code:
    http://wordpress.pastebin.ca/2148417

    I'm pulling my hair out here. Why do custom post type taxonomies have to be so complicated? :-(

    Many thanks,

    Adam.

  2. flint_and_tinder
    Member
    Posted 1 year ago #

    A-ha, I have solved it myself. Thanks anyway to anyone who reads this.

  3. Chris
    Member
    Posted 1 year ago #

    Can you post your solution for others?

  4. flint_and_tinder
    Member
    Posted 1 year ago #

    Hi Chris,

    I managed it like the below (which I think is similar to the original paste bin code, but without slight errors), for each loop in the template you just need something like the following. This example is for the fan-comments loop. For the fan-merchandise loop I changed the term field and $comments identifiers to suit.

    <?php $args=array(
    	'taxonomy' => 'fan_post_types',
    	'term' => 'fan-comments',
    	'post_type' => 'fan',
    	'posts_per_page' => 8
    	);
    	$comments = new WP_Query($args);
        if($comments->have_posts()) : while($comments->have_posts()) :
    	$comments->the_post(); ?>
    
    Whatever you want to display goes here...
    
      <?php endwhile; endif; ?>
      <?php wp_reset_query(); // end Fan Cooments loop. ?>

    Let me know if you need any more info than that.

  5. flint_and_tinder
    Member
    Posted 1 year ago #

    Second loop:

    <?php $args=array(
    	'taxonomy' => 'fan_post_types',
    	'term' => 'fan-merchandise',
    	'post_type' => 'fan',
    	'posts_per_page' => 8
    	);
        $merchandise = new WP_Query($args);
        if($merchandise->have_posts()) : while($merchandise->have_posts()) :
    	$merchandise->the_post(); ?>
    
    Whatever you want to display goes here...    	
    
      <?php endwhile; endif; ?>
      <?php wp_reset_query(); // end Fans Merchandise loop. ?>
  6. flint_and_tinder
    Member
    Posted 1 year ago #

    Both those loops site within the main page loop like so...

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <?php the_content(); ?>
    
    Loop 1
    
    Loop 2
    
    <?php endwhile; endif; ?>
  7. rightmost
    Member
    Posted 1 year ago #

    Thank you! This worked for me.

Topic Closed

This topic has been closed to new replies.

About this Topic