0

I have created custom post type - 'portfolio' with custom taxonomy designating the category and subcategory.

I have an example structure:
- main_category:
-- subcategory_1
-- subcategory_2

The problem is as following: If I check main_category and subcategory_1, WP sees correct hierarchy, but if I choose main_category and subcategory_2 to post, WP sees subcategory_2 as main_category and doesn't see parent category. This is really strange, because this situation is only when I choose subcategory_2, I checked this with get_the_terms() function.
What is wrong?
My register code:

// Register Custom Post Type
function portfolio_post_type() {

$labels = array(
    'name'                  => _x( 'Portfolio', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Portfolio', 'Post Type Singular Name', 'text_domain' ),
);
$args = array(
    'label'                 => __( 'Portfolio', 'text_domain' ),
    'description'           => __( 'Portfolio', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'thumbnail', 'page-attributes' ),
    'hierarchical'          => false,
    'public'                => true,
    'has_archive'           => false,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'rewrite'               => array( 'slug' => 'portfolio/%kategoria%', 'with_front' => false )
);
register_post_type( 'portfolio', $args );

}
add_action( 'init', 'portfolio_post_type', 0 );

// Register Custom Taxonomy
function portfolio_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Kategorie', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Kategoria', 'Taxonomy Singular Name', 'text_domain' ),
        'add_new_item'               => __( 'Dodaj nową kategorię', 'text_domain' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'rewrite'                    => array( 'slug' => 'portfolio', 'with_front' => false )
    );
    register_taxonomy( 'kategoria', array( 'portfolio' ), $args );

}
add_action( 'init', 'portfolio_taxonomy', 0 );
  • Why use 0 for priority ? Just remove it. – Beee Feb 25 '18 at 23:59
  • @Beee you mean: "add_action( 'init', 'portfolio_taxonomy', 0 )" and "add_action( 'init', 'portfolio_post_type', 0 )" - 0 at the end? – Marek Feb 26 '18 at 9:12
  • @Beee I removed 0 and still nothing changed – Marek Feb 26 '18 at 9:59
  • Does your debug log show anything ? – Beee Feb 26 '18 at 10:52
  • @Beee Nothing appears in the debug. I'm a bit more precise: when I display the information using var_dump (get_the_terms (post_ID, taxonomy)) - in the post assigned to subcategory_2 first element in array is main_category, then subcategory_2. In the case of a post assigned to subcategory_1, the first element in the array is subcategory_1, then main_category. Does this information add anything? – Marek Feb 26 '18 at 11:39

Your Answer

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

Browse other questions tagged or ask your own question.