0

I have a custom post type that has a custom taxonomy which functions as the main hierarchical structure for this custom post type. I am experiencing some behavior that I can't explain though.

This hierarchical structure is 3 levels deep as following:

main
-section
--subsection

The idea is that every custom post type is added to a subsection so that a book>chapter-like structure forms. I have made several restrictions to the custom post type admin page so that it's only possible to add new custom post types to subsections (and this part is working well).

The problem is that for main/sections filters the custom post type that are in certain subsections won't show. This isn't just in my custom frontend archives but also in the WordPress backend, which makes it seem like quite a fundamental problem. For the backend of this custom post type I have also added some pretty standard filters for the taxonomies. When I select the --subsection depth in this filter, the posts that have this --subsection show as expected. But when select the main or -section of the aforementioned --subsection, nothing shows. All of these taxonomies have been added by a user, so I'm not sure what or if he has done something differently with adding these problematic --subsections.

Some plugins that could be related and are active:

  • WPML Multilingual CMS
  • Category Order and Taxonomy Terms Order
  • CPT-onomies: Using Custom Post Types as Taxonomies

Naturally I've tried disabling them.

My CPT registration:

add_action('init', function() {
    global $plugin_base_dir;

    // Argumenten voor registreer functie
    $args = array(
        'labels' => array(
            'name' => 'Rules',
            'singular_name' => 'Rule',
            'add_new' => 'New rule',
            'add_new_item' => 'Add new rule',
            'edit_item' => 'Edit rule',
            'new_item' => 'New rule',
            'view_item' => 'View rule',
            'search_items' => 'Search rules',
            'not_found' => 'No rules found',
            'not_found_in_trash' => 'No rules found in trash'
        ),
        'query_var' => 'rule',
        'rewrite' => false, 
        'taxonomies' => array(
            'rlt_rule_headline', 
            'rlt_rule_topic',
            'rlt_rule_year'
        ),
        'public' => true,
        'publicly_queryable' => true,
        'menu_icon' => '',
        'show_ui' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array(
            'title',
            'editor'
        )
    );
    // Registreer de Custom Post Type
    register_post_type('rlt_rule', $args);
});

The problematic taxonomy registration:

register_taxonomy(
    'rlt_rule_headline',
    'rlt_rule',
    array(
        'labels' => array(
            'name' => 'Headlines',
            'singular_name' => 'Headline',
            'add_new_item' => 'Add headline',
            'new_item_name' => "New headline"
        ),
    'rewrite' => array(
        'slug' => 'headline',
        'with_front'=> false,  
        'feed'=> true 
        ),
    'show_ui' => true,
    'show_tagcloud' => false,
    'hierarchical' => true
    )
);

My added backend filter:

$filter_taxonomy = 'rlt_rule_headline';
    wp_dropdown_categories( 
        array(
            'show_option_all' => 'Show all headlines',
            'taxonomy' => $filter_taxonomy,
            'name' => $filter_taxonomy,
            'orderby' => 'name',
            'selected' => ( isset( $wp_query->query[$filter_taxonomy] ) ? $wp_query->query[$filter_taxonomy] : '' ),
            'hierarchical' => true,
            'depth' => 3,
            'show_count' => false,
            'hide_empty' => true,
        ) 
    );

The filter is default. I haven't added a function to catch it and handle anything. So it just adds query_vars and filters like a normal archive. The same goes with the frontend archive.

  • Could post the code you are using to register the custom post type, the custom taxonomy and the query to get the posts? – cybmeta Sep 16 '13 at 14:32
  • I have added them and a comment to explain further. – GMBrian Sep 16 '13 at 14:57
0

What you are calling "filter" is just a HTML select element. You need the put the select element in a form and filter the posts based on the selected option of this select element.

For example, for the frontend:

The form:

<form method="post" action="<?php echo get_post_type_archive_link('rlt_rule');?>">
    <?php
    $filter_taxonomy = 'rlt_rule_headline';
    wp_dropdown_categories( 
        array(
            'show_option_all' => 'Show all headlines',
            'taxonomy'        => $filter_taxonomy,
            'name'            => $filter_taxonomy,
            'orderby'         => 'name',
            'selected'        => ( isset( $wp_query->query[$filter_taxonomy] ) ? $wp_query->query[$filter_taxonomy] : '' ),
            'hierarchical'    => true,
            'depth'           => 3,
            'show_count'      => false,
            'hide_empty'      => true,
        ) 
    );
    <button type="submit">Search</button>
</form>

The filter process:

add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){

   //limit to main query and frontend are. Add you own conditionals
   if($query->is_main_query() && !is_admin()) {

       $tax_query = array();
       $tax_input = isset($_POST['rlt_rule_headline']) ? $_POST['rlt_rule_headline'] : '';
       if(!empty($tax_input)){

           $tax_query[] = array(
                                'taxonomy'         => 'rlt_rule_headline',
                                'field'            => 'id',
                                'terms'            =>  $tax_input,
                                'operator'         => 'IN',
                                'include_children' => true
                               );
        }
    }

    $query->set('tax_query',$tax_query);
}

For the backend you to include your filter in the restrict_manage_posts action hook as seen in this answer.

0

It turned out to be related to the plugin CPT-onomies. Naturally I tried disabled plugins to test. But these archives that caused issues also relied on some plugins to function and I didn't disabled plugins independently.

Lesson learned. I didn't research the plugin properly enough before using it. It's kind of weird since I only used this plugin to add a 'CPT-onomy' to another CPT's archive. But as far as I know there's no errors in the plugin.

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.