Support » Plugins » Hacks » Custom Taxonomy Template not respecting ‘include_children’ => ‘false’

  • I’ve created a custom taxonomy, for a custom post type, and created a custom page for it, the usual, etc.

    Problem is I want to only show the custom posts that are part of the category, and not show the posts from the sub categories. So I wrote the following query for the loop:

    <?php global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'product', 'include_children' => 'false' ) );
    query_posts( $args );
    if(have_posts()) : while(have_posts()) : the_post(); ?>

    Although 'include_children' => 'false' is included in the $args it still shows the products from the subcategories. I tried changing it for 'post_parent' => 0, and using them both at the same time, but to no avail.

    Here is the code for my taxonomy:

    function productcat_taxonomy() {
    
      $labels = array(
        'name' => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Product Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Product Categories' ),
        'all_items' => __( 'All Product Categories' ),
        'parent_item' => __( 'Parent Product Category' ),
        'parent_item_colon' => __( 'Parent Product Category:' ),
        'edit_item' => __( 'Edit Product Category' ), 
        'update_item' => __( 'Update Product Category' ),
        'add_new_item' => __( 'Add New Product Category' ),
        'new_item_name' => __( 'New Product Category Name' ),
        'menu_name' => __( 'Product Categories' ),
      );    
    
      register_taxonomy('product-category',array('product'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-category' ),
      ));
    
    }
    
    add_action( 'init', 'productcat_taxonomy', 0 );

    Where have I gone wrong? Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The ‘include_children’ argument needs to be part of the ‘tax_query’ array. Actually, in same level array as the taxonomy, field, terms, etc. arguments. Adding it to the main arguments array like you did will have no effect. The nested arrays are confusing, but it’s important to get the argument in the right array level.

    The post parent idea is the wrong approach, that is for hierarchical posts. You are trying to query using a taxonomy hierarchy.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Taxonomy Template not respecting ‘include_children’ => ‘false’’ is closed to new replies.