WordPress.org

Ready to get started?Download WordPress

Forums

List posts by the terms for a custom taxonomy of any post type (1 post)

  1. maestro2012
    Member
    Posted 1 year ago #

    Hi.

    I found this loop, and i have small problem with that.

    I have some parent category.
    And the parent has children.

    Parent Taxonomy
    Parent Taxonomy Post 1
    Parent Taxonomy Post 2

    Child Taxonomy 1
    - Child Taxonomy 1 Post 1
    - Child Taxonomy 1 Post 2
    - Child Taxonomy 1 Post 3

    Child Taxonomy 2
    - Child Taxonomy 2 Post 1
    - Child Taxonomy 2 Post 2
    - Child Taxonomy 2 Post 3

    But this not working properly.
    In parent Taxonomy i have children taxanomy posts.

    Parent Taxonomy
    Parent Taxonomy Post 1
    Parent Taxonomy Post 2
    - Child Taxonomy 1 Post 1
    - Child Taxonomy 1 Post 2
    - Child Taxonomy 1 Post 3
    - Child Taxonomy 2 Post 1
    - Child Taxonomy 2 Post 2
    - Child Taxonomy 2 Post 3

    I want have to in parent taxonomy, posts only for parent taxonomy, without child taxonomy posts.
    And in child taxonomy, only posts for child taxonomy.

    This is OK:

    Parent Taxonomy
    Parent Taxonomy Post 1
    Parent Taxonomy Post 2

    Child Taxonomy 1
    - Child Taxonomy 1 Post 1
    - Child Taxonomy 1 Post 2
    - Child Taxonomy 1 Post 3

    Child Taxonomy 2
    - Child Taxonomy 2 Post 1
    - Child Taxonomy 2 Post 2
    - Child Taxonomy 2 Post 3

    This is wrong:

    Parent Taxonomy
    Parent Taxonomy Post 1
    Parent Taxonomy Post 2
    Child Taxonomy 1 Post 1
    Child Taxonomy 1 Post 2
    Child Taxonomy 1 Post 3
    Child Taxonomy 2 Post 1
    Child Taxonomy 2 Post 2
    Child Taxonomy 2 Post 3

    Child Taxonomy 1
    - Child Taxonomy 1 Post 1
    - Child Taxonomy 1 Post 2
    - Child Taxonomy 1 Post 3

    Child Taxonomy 2
    - Child Taxonomy 2 Post 1
    - Child Taxonomy 2 Post 2
    - Child Taxonomy 2 Post 3

    How fix this problem?
    Sorry for my english...

    <?php
    
    // List posts by the terms for a custom taxonomy of any post type
    
    $post_type = 'posttypename';
    $tax = 'taxonomyname';
    $tax_terms = get_terms( $tax );
    if ($tax_terms) {
    	foreach ($tax_terms  as $tax_term) {
    	$args = array(
    		'post_type' => $post_type,
    		"$tax" => $tax_term->slug,
    		'post_status' => 'publish',
    		'orderby' => 'title',
    		'order' => 'ASC',
    		'posts_per_page' => -1,
    		'caller_get_posts'=> 1
    	);
    
    		$my_query = null;
    		$my_query = new WP_Query($args);
    
    		if( $my_query->have_posts() ) : ?>
    
    			<h2 class="produktgrupp"><?php /*?>All <?php echo $tax; ?> Posts For <?php */?><?php echo $tax_term->name; ?></h2>
    			<ul class="produktlistning">
    			<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    
    				<li id="post-<?php the_ID(); ?>">
    					<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    					<span><?php the_post_thumbnail('produktlistning-thumbnail'); ?></span>
                        <h3><?php the_title(); ?></h3>
                        </a>
    
    				</li>
    
    			<?php endwhile; // end of loop ?>
    
    			</ul>
    
    		<?php else : ?>
    		<?php endif; // if have_posts()
    		wp_reset_query();
    
    	} // end foreach #tax_terms
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic