3

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?

  • 2
    I don't know if it matters in WP, but the value is supposed to be a boolean, and you are passing false as a string. – gdaniel Sep 14 '16 at 14:02
  • @gdaniel thanks for that - although it unfortunately makes no difference - an error all the same – CalvT Sep 14 '16 at 14:05
  • @hkchakladar thanks for the link, but strangely enough I can't seem to make it work. Care to post an answer with more detail? :) – CalvT Sep 14 '16 at 19:11
1

There was a lot of hard coding going on in a number of these answers that I felt like may have disadvantages further down the road. Here is an approach that takes the existing tax_query, and swaps out the "include_children" argument.

As an aside, I did not have any luck setting "include_children" to false in this instance. I only found a zero to work.

function wpse239243_exclude_child_taxonomies( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        if (is_tax('product-category')) {
            $tax_query = $query->tax_query->queries;
            $tax_query[0]['include_children'] = 0;
            $query->set( 'tax_query', $tax_query );
        }
    }
    return;
}
add_action( 'pre_get_posts', 'wpse239243_exclude_child_taxonomies', 1 );
  • This is the solution that works. – rfair404 Dec 13 '19 at 0:29
1

Try this:

  1. As @gdaniel said, you are giving a string in place of boolean value. Replace 'include_children' => 'false' to 'include_children' => false (Remove quote from false)
  2. You can hook into the pre_get_posts to change the query vars before getting the posts. And when on custom taxonomy template page, include your custom query to the global query. This goes to themes functions.php

function wpse239243_exclude_child( $query ) {

    //if on frontend custom taxonomy template
    if( $query->is_tax( 'product-category' ) && $query->is_main_query() && !is_admin() ) {

        $tax_query = array( array(
            'taxonomy'         => 'product-category',
            'terms'            => $query->get( 'product-category' ),
            'include_children' => false
        ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'wpse239243_exclude_child' );
  • Point 1 I tried before (as noted in the comments) and it makes no change. Point 2 causes an Error 500 when enabled. Any ideas? – CalvT Sep 16 '16 at 12:00
0

You have used 'include_children' => 'false', but you haven't told WordPress which taxonomy this is about and also not what the base category is you don't want the children of. You would do that as follows:

array(
    'post_type' => 'product',
    'tax_query' => array( array(
        'taxonomy'         => 'product-category',
        'include_children' => 'false' 
        'terms'            => 'base-category-ID',
    ) ),
);

Instead of a fixed base-category-ID, you can off course use a variable containing the name of the first category on the current post:

$categories = get_the_category();
$category_id = $categories[0]->cat_ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.