WordPress.org

Ready to get started?Download WordPress

Forums

Display only children posts within a custom taxonomy (4 posts)

  1. w3r3w0lf
    Member
    Posted 3 years ago #

    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 :-)

  2. w3r3w0lf
    Member
    Posted 3 years ago #

    anyone?

  3. lewismcarey
    Member
    Posted 3 years ago #

    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

  4. lewismcarey
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic