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'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?

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?

Share Improve this question asked Sep 14, 2016 at 13:39 CalvTCalvT 3342 gold badges8 silver badges15 bronze badges 4
  • 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 Commented Sep 14, 2016 at 14:02
  • @gdaniel thanks for that - although it unfortunately makes no difference - an error all the same – CalvT Commented Sep 14, 2016 at 14:05
  • See here wordpress.stackexchange/questions/169673/… – hkchakladar Commented Sep 14, 2016 at 17:07
  • @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 Commented Sep 14, 2016 at 19:11
Add a comment  | 

3 Answers 3

Reset to default 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 );

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' );

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745616920a4636290.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信