0

I created a custom post type product and added a taxonomy product_category for it, with hierarchical set to true since I wanted parent categories (use case: fruits > berries etc). The admin list for product (/wp-admin/edit.php?post_type=product) only shows products with a parent taxonomy but none with a child taxonomy, even though the counter for products shows the right number.
When navigating to /wp-admin/edit.php?product_category=berries&post_type=product, it shows those missing posts.

What am I doing wrong? Below is the responsible code:

$taxonomy = [
  'hierarchical'          => true,
  'labels'                => [ ... ],
  'show_ui'               => true,
  'show_admin_column'     => true,
  'update_count_callback' => '_update_post_term_count',
  'query_var'             => true,
  'rewrite'               => true,
  'refersTo'              => 'product'
];

$postType = [
    'labels'      => [ ... ],
    'supports'    => [
    'title',
    'editor',
    'thumbnail',
    'custom-fields',
    'revisions'
  ],
  'taxonomies'  => [ 'product_category' ],
  'rewrite'     => true,
  'public'      => true,
  'has_archive' => true
];

function flush_rewrite_rules () {
  global $wp_rewrite;
  $wp_rewrite->flush_rules();
});
add_action('init', 'flush_rewrite_rules' );

function register_products_taxonomy () {
  register_taxonomy( 'product_category', 'product', $taxonomy);
  flush_rewrite_rules();
}
add_action( 'init', 'register_products_taxonomy', 5 )

function register_products_type () {
  register_post_type( 'product', $postType );
  flush_rewrite_rules();
}
add_action( 'init', 'register_products_type' );
  • For sure it should be: register_post_type( 'product', $postType ); Something is wrong with the pasted code, there are more mistakes like: 'rewrite' => true 'public' => true, instead of 'rewrite' => true, 'public' => true, Code like this wouldn't parse - please check once again the code sample and correct the obvious errors. – Picard May 11 '17 at 21:55
  • Yep, the original code is more complicated, so these are errors from copy pasting since the theme runs normally currently – Mo Friedrich May 11 '17 at 21:59
  • Sorry, hit submit too soon - the original code is way larger, but the code excerpts above are what it boils down to. I've corrected the errors. – Mo Friedrich May 11 '17 at 22:03
  • @Picard, any idea on this? – Mo Friedrich May 12 '17 at 16:11
0

It is not exactly the answer but here it will get formatted better than in the comments section.

To solve your problem I would start with the simplest possibile working set. Then line by line I would extend the solution checking if it still works after each step. You could start with:

function custom_post_types() {
    $args = array(
        'public' => true,
        'label'  => 'Products'
    );
    register_post_type( 'product', $args );

    $args = array(
        'label' => 'Product category',
        'rewrite' => array( 'slug' => 'product_category' ),
        'hierarchical' => true,
    );
    register_taxonomy( 'product_category', 'product', $args );
}
add_action('init', 'custom_post_types', 0);

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.