Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a project where I have a custom taxonomy, for example, called class-formation, that is shared for two Custom Post Types. On of them is the product CPT created by WooCommerce, and the another one is custom made for the project.

I can assing the taxonomy to both CPT's. But the problem is when I try to access the taxonomy archive for class-formation, I didn't got a 404 not found error, but none posts are returned (have_posts() returns false) . And a weird thing is happening too: on this page, every custom menu that I have generated using wp_nav_menu is missing (I tested with some themes to check if was a theme related issue, but the behavior is the same).

What I'm doing:

in my functions.php

<?php

// registering the taxonomy "class-formation"
function theme_register_taxonomy(){

    $labels = apply_filters( 'tax_class_formation_labels' ,array(
        'name'                       => _x( 'Formações Acadêmicas', 'Taxonomy General Name', 'theme_slug' ),
        'singular_name'              => _x( 'Formação Acadêmica', 'Taxonomy Singular Name', 'theme_slug' ),
        'menu_name'                  => __( 'Formações', 'theme_slug' ),
        'all_items'                  => __( 'Todos as Formações', 'theme_slug' ),
        'parent_item'                => __( 'Formação Pai', 'theme_slug' ),
        'parent_item_colon'          => __( 'Formação Pai:', 'theme_slug' ),
        'new_item_name'              => __( 'Novo nome de Formação', 'theme_slug' ),
        'add_new_item'               => __( 'Adicionar nova Formação', 'theme_slug' ),
        'edit_item'                  => __( 'Editar Formação', 'theme_slug' ),
        'update_item'                => __( 'Atualizar Formação', 'theme_slug' ),
        'separate_items_with_commas' => __( 'Separe formações acadêmicas com vírgulas', 'theme_slug' ),
        'search_items'               => __( 'Pesquisar formações', 'theme_slug' ),
        'add_or_remove_items'        => __( 'Adiciona ou remova formações', 'theme_slug' ),
        'choose_from_most_used'      => __( 'Escolha entre as formações mais usadas', 'theme_slug' ),
    ) );

    $rewrite = array(
        'slug'                       => apply_filters( 'tax_class_formation_slug', _x( 'formacao-academica', 'Taxonomy Slug', 'theme_slug' ) ),
        'with_front'                 => true,
        'hierarchical'               => false,
    );

    $args = apply_filters( 'tax_class_formation_args', array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'query_var'                  => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'rewrite'                    => $rewrite,
    ) );

    register_taxonomy( 'class-formation', array( 'theme-courses', 'product' ), $args );

}

add_action( 'init', 'theme_register_taxonomy' );

// filtering the archive taxonomy page to get posts with "theme-courses" CPT
add_action( 'pre_get_posts', 'filter_tax_archive', 1 );

function filter_tax_archive( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( $query->is_main_query() && $query->is_tax( 'class-formation' ) ) {
        $query->set( 'post_type', array( 'theme-courses' ) );
        return;
    }
}

The post_type query var is successfully inserted, but I got an empty loop anyway.

Any ideas for what's causing this issue?

Thanks for any help!

Edit:

The code above is a simplified version of all the code that I'm using. I made a gist with the full code if anyone want to check: https://gist.github.com/diegoliv/274f09e3f3d9094b204c

Edit 02:

Maybe there is some conflict with WooCommerce? If I replace the product CPT with another one when I'm registering the taxonomy, the taxonomy page works ok. The same happens if I disable WooCommerce. But, with the actual code, when I'm inspecting the global $wp_query to check all the arguments used to build the query, there is some arguments coming from WooCommerce.

share|improve this question
    
Is that the real code above? Because if so, then you should wrap the registration of the CT in a callback attached to a hook... –  kaiser May 21 at 21:53
    
Hi, @kaiser! Sorry, I adapted the code because I'm using an OPP approach to my code and I wanted to simplify the code to show here. Actually, the 'class-formation' CT and the 'theme-courses' CPT are both created by a custom plugin. –  Diego de Oliveira May 21 at 22:02
    
If it's not working, please show your real code (if you haven't done that by your last edit). Thanks :) –  kaiser May 21 at 22:10
    
@kaiser, I didn't, sorry! I've updated the question with a gist that contains all the code of my plugin. I moved the pre_get_posts action hook to the plugin as well. There's some more CPT's and CT's, but the only one CT that is shared by two or more CPT's is the one named tesla-formacao. Thanks for the help! –  Diego de Oliveira May 21 at 22:30
    
A last note: Please do not link to outside sources. If the link is gone, no one will understand what you were asking about. Thanks. –  kaiser May 21 at 23:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.