WordPress.org

Ready to get started?Download WordPress

Forums

Show all custom post types organized by custom taxonomy (10 posts)

  1. rhetthenckel
    Member
    Posted 1 year ago #

    I'm building a wordpress powered site for a theatre company. I'm using the types plugin and have created a custom post type 'productions' and organizing them by a custom taxonomy 'seasons.' I'm building a custom template for a page that should display every show organized by season. I've tried many things and still can't get it to work. Here's the code I'm using now. Any help is appreciated!

    <?php
    //get all seasons then display all posts in each term
    $taxonomy = 'seasons';
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'seasons',
    			'field' => 'slug',
    			'terms' => array($term->term_id)
    		)
    	)
    	);
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
    		<div class="season section">
    			<h2><?php echo $term_name; ?></h2>
    			<ul>
    				<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    			        <li>
    			        <a style="background-image: url(
    			        	<?php
    				        	//Get the Thumbnail URL
    					        $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 300,160 ), false, '' );
    						    echo $src[0];
    						?>
    						);" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    							<div class="inner-block"
    							<h3><?php the_title(); ?></h3>
    							<span class="playwright"><?php echo(types_render_field("playwright", array("raw"=>"true"))); ?></span>
    							<span class="director"><?php echo(types_render_field("director", array("raw"=>"true"))); ?></span>
    							<span class="dates"><?php echo(types_render_field("date", array("style"=>"text"))); ?></span>
    							</div>
    			        </a>
    			        </li>
    
    			    <?php endwhile; ?>
    			</ul>
    		</div>
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  2. rhetthenckel
    Member
    Posted 1 year ago #

  3. Anthony D'Arco
    Member
    Posted 1 year ago #

    I had the same problem. I did a ton of research, and honestly, I couldn't find an answer, however, I tried copy and pasting from your link and then copied the tax_query from this post: http://wordpress.stackexchange.com/questions/49185/tax-query-parameter-not-working-with-wp-query

    Some how it worked...I honestly don't know what the difference was, but here was my raw code:

    <?php
    //get all categories then display all posts in each term
    $taxonomy = 'vc-memorial';
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          'post_type' => 'vc-memorial-us',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1,
    	  'tax_query' => array(
    			array(
    				'taxonomy' => 'vc-memorial',
    				'terms' => array($term->term_id),
    				'include_children' => true,
    				'operator' => 'IN'
    			)
    		)
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
          <div class="category section">
    	    <h3><?php echo 'Category '.$term->name;?></h3>
    	    <ul>
    	    <?php
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
            <div><?php the_excerpt ?></div>
           <?php
          endwhile;
          ?>
          </ul>
          </div>
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Here are the final results with just adding some design to it: http://viatorians.wpengine.com/resources/in-memoriam/

    I hope that helps.

    Thank you,
    Anthony

  4. M Szatkowski
    Member
    Posted 1 year ago #

    Fine but how would you add the above code to category.php and display current category post types divided by current category children?

  5. Anthony D'Arco
    Member
    Posted 1 year ago #

    I'm not sure if I understand your question. If you are using category.php you are only going to be able to sort the categories within the posts. The purpose of the code above is for custom post types with custom taxonomies, so the above code would need to added to some other template than your category.php.

    If you are looking to do this for each category (for Posts only), then this resource should help you out: http://eisabainyo.net/weblog/2010/03/10/display-5-latest-posts-in-each-category-in-wordpress/

    I hope that helps,
    Anthony

  6. M Szatkowski
    Member
    Posted 1 year ago #

    Thanks Anthony but it's not what I expected.
    You have provided me the query on the page.

    I need to list post types from current category divided by category like:

    cat1
    - post1
    - post2

    cat2
    -post3
    -post4

    Now I list all posts like:

    <ul class="categorylist">
    <?php
       $catID = get_query_var('cat');
       $catName = strtolower(get_cat_name($catID));
        if ( get_query_var('paged') ) {
            $paged = get_query_var('paged');
        } elseif ( get_query_var('page') ) {
            $paged = get_query_var('page');
        } else {
            $paged = 1;
        }
    
    	$myquery = new WP_Query(array(
        'post_type' => 'products',
        'cat' => $catID)
        );
       //echo'<pre>';print_r($myquery);echo'</pre>';
        while ( $myquery->have_posts() ) : 
    
            $myquery->the_post();
            $img_id = get_post_meta($post->ID,'thumbnail',true);
            $img_url = wp_get_attachment_url($img_id);
            $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            $cat = get_the_category();
            $parentCatName = get_cat_name($cat[0]->parent);
            $prodCatName = get_cat_name($cat[0]);
            ?>
            <li>
                <?php $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID )); ?>
                <div class="categorylistimg">
    
    <?php
        // If post thumbnail exists, if not use this image
        if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
    
       <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo $parentCatName; ?> - <?php the_title(); ?>">
                    <img alt="<?php the_title(); ?>" title="<?php echo $parentCatName; ?> - <?php the_title(); ?>" data-imgw="50" src="<?php bloginfo('template_url'); ?>/thumb.php?src=<?php echo $src[0]; ?>&h=50&w=50&zc=2&q=90" />
                </a> 
    
    			<?php }  else {
            echo '<img src="/wp-content/uploads/2013/04/default.jpg" alt="noimage" title="no image"/>';
        }
    ?>
               </div>
    			  <div class="categorylisttitle">
                <strong><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php echo $parentCatName; ?> - <?php the_title(); ?>"><?php the_title(); ?></a></strong>
    
    <span class="excerptprod">
    <?php $excerpt = strip_tags(get_the_excerpt());
            echo $excerpt; ?>
    </span>
    		</div>
            </li>
        <?php endwhile; ?>
    </ul>
  7. Anthony D'Arco
    Member
    Posted 1 year ago #

    Mariusz,

    Overall, I'm a bit confused. It maybe just an issue of semantics, but is enough for me not to understand the question. When you say, "I need to list post types from current category divided by category..." Are we dealing with custom post types/taxonomies? If so, the all the functions associated with categories, don't work. (Please someone correct me if my this is incorrect), the category functions only work with posts and not custom post types.

    So, my original code should work for you if you are working with custom post types/taxonomies, but won't work if they are simply categories with the post section. For the categories the link I included should work (although I did not test it out).

    Let me know if that helps.

    Thank you,
    Anthony

  8. jennybeaumont
    Member
    Posted 1 year ago #

    @adarco - this is huge! just what I was looking for, thank you so much!
    -jennyb

  9. jennybeaumont
    Member
    Posted 1 year ago #

    any chance you a trick for excluding child taxonomies from the list? setting "include_children" to "false" does not seem to do the trick...

    thnx!
    -jennyb

  10. jennybeaumont
    Member
    Posted 1 year ago #

    nevermind, got it. added 'parent' => 0 to the term_args and all is well :)

    thanks again,
    -jennyb

Topic Closed

This topic has been closed to new replies.

About this Topic