0

When I attempt to filter a custom post type's admin table(CPT's posts) using the select dropdown, the filtered table that loads shows 0 items.

This is the url structure that is being created by submitting the filter table form (I've added linebreaks for readability):

{HOST}/wp-admin/edit.php
?s
&post_status=all
&post_type=dog
&action=-1
&dog=labrador
&filter_action=Filter
&paged=1
&mode=list
&action2=-1

There are no errors, I've tried numerous tutorials and countless tweaks and can't seem to get anything to take. Anyone have any idea what might be going on?

I can add any filters, actions, functions, declarations I've used to this question if they would be of use -- just let me know what would be helpful to see.

  • ...the same holds true if I attempt to view a filtered table using the links related to the taxonomy, eg: {HOST}/wp-admin/edit-tags.php ?action=edit &taxonomy=dog &tag_ID=29 &post_type=dog – Dub Tempo Sep 8 '14 at 21:51
  • 1
    Can you supply a pastebin with your custom post type and taxonomy registration code? – DrewAPicture Sep 8 '14 at 22:42
  • Certainly, here is a link. – Dub Tempo Sep 8 '14 at 23:48
1

Well, the short answer is that your taxonomies and custom post types share slugs, which is what is causing the list table to display nothing. If you were to change the taxonomy slugs from $newType to $newType . '_tax' it should work. I actually used that same slug formula in the rewrites example below.

If you're really just trying to save time by using the same slugs, simply make the slugs unique for post_type vs taxonomy and ignore the rest of this answer.

However, if you're using the same slugs in an attempt to share the front-end rewrite slugs, e.g. example.com/dog/labrador (term) vs example.com/dog/this-is-a-dog-item (post), the following should work for that in some measure:

/**
 * Match taxonomy term rewrite slugs with custom post type slugs.
 *
 * @param WP_Rewrite $wp_rewrite WP_Rewrite global.
 */
function match_tax_rewrites_to_cpt( $wp_rewrite ) {
    $pairs = array();

    // Set up post type and taxonomy pairs.
    foreach ( array( 'dog', 'cat', 'fish', 'bird' ) as $slug ) {
        $post_type = get_post_type_object( $slug );
        $taxonomy  = get_taxonomy( $slug . '_tax' );
        if ( is_object( $post_type ) && is_object( $taxonomy ) ) {
            $pairs[ $slug ] = array(
                'post_type' => $post_type,
                'taxonomy'  => $taxonomy
            );
        }
    }

    if ( ! empty( $pairs ) ) {
        $rules = array();

        // Loop through the pairs.
        foreach ( $pairs as $slug => $objects ) {

            // Check if taxonomy is registered for this custom type.
            if ( ! empty( $tax_obj_type = $objects['taxonomy']->object_type[0] ) ) {
                if ( $tax_obj_type == $slug ) {

                    // Term objects.
                    $terms = get_categories( array(
                        'type'       => $tax_obj_type,
                        'taxonomy'   => $objects['taxonomy']->name,
                        'hide_empty' => false
                    ) );

                    // Build your rewrites.
                    foreach ( $terms as $term ) {
                        $rules[ "{$tax_obj_type}/{$term->slug}/?$" ] = add_query_arg( $term->taxonomy, $term->slug, 'index.php' );
                    }
                }
            }
        }
        // Merge the rewrites.
        $wp_rewrite->rules = $rules + $wp_rewrite->rules;
    }
}
add_action( 'generate_rewrite_rules', 'match_tax_rewrites_to_cpt' );

It's worth noting that if you use this function to generate rewrite rules, it's actually important that you move the register_post_type() calls above the register_taxonomy() calls. This is to ensure that the base rewrite rules generated by WordPress end up in the right order.

  • The name conflict was precisely the issue. Interestingly, I only noticed the issues when I started looking at filtering. The getting terms and taxonomies functions worked without problems. For anyone else in this boat, my route was to update the name used for the taxonomy, as per @drew 's answer. Then, rename the taxonomies in the db. The second paragraph of the register taxonomy codex page, does mention the name-conflict issue. – Dub Tempo Sep 10 '14 at 0:14

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.