• Hi

    I am creating a site with a custom taxonomy, and custom post types. The could be several levels to the taxonomy, with the lowest level containing the custom post types (the products).

    E.g.
    Level 1 > Level 2a > products
    Level 1 > Level 2b > products

    The problem I am having is that I can’t hide the products when I am viewing Level 1. For some reason it is displaying the products from Level 2a and 2b as well.

    This is the code I am using:

    <?php $recent = new WP_Query(“post_type=product&customtaxonomy=$slug&posts_per_page=$posts_per_page”); ?>
    <?php while($recent->have_posts()) : $recent->the_post();?>

    Many thanks for any tips 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter w3r3w0lf

    (@w3r3w0lf)

    anyone?

    I am using this within a taxonomy-taxname.php:

    $term = $wp_query->get_queried_object();
    	$termID = $term->term_id;
    	$taxonomyName = "directories";
    	$termchildren = get_term_children( $termID, $taxonomyName );
    
    	$args = array(
    		'posts_per_page' => -1,
    		'tax_query' => array(
    			'relation' => 'AND',
    			array(
    				'taxonomy' => 'directories',
    				'field' => 'id',
    				'terms' =>$term->term_id,
    			),
    			array(
    				'taxonomy' => 'directories',
    				'field' => 'id',
    				'terms' => $termchildren,
    				'operator' => 'NOT IN',
    			)
    		)
    	);
    	query_posts( $args );

    The reference for this code is on http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters

    This gets the children of the current directory (E.g. Level 1)
    Then excludes them from the query.
    That way when you are in Level 1 it will only show Level 1 posts, when you are in sub directory Level2a it will show only those ones.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display only children posts within a custom taxonomy’ is closed to new replies.