0
votes

I have created a Custom Post Type in WordPress called "Found". Inside that, I have created two taxonomies, "Pets" (named petsfound) and "Electronics" (named electronicsfound) and both of those taxonomies have various terms.

If I view the posts that use the taxonomy terms on the site, they show correctly, however, attempting to view a list of posts for the taxonomy shows a 404. So, the following happens:

https://example.com/found/pets-found/ shows a 404 page.

https://example.com/found/pets-found/dog shows the list of dogs in the pets found taxonomy.

I have tested with using both archive.php and taxonomy-petsfound.php but both show the 404 for the taxonomy. This is the same for the electronics taxonomy too.

Below is the code for the "Found" CPT and "petsfound" Taxonomy:

function found_custom_post_type() {
    $labels = array(
        'name'                  => _x( 'Found Items', 'Post Type General Name', 'lost_and_found' ),
        'singular_name'         => _x( 'Found', 'Post Type Singular Name', 'lost_and_found' ),
        'menu_name'             => __( 'Found', 'lost_and_found' ),
        'name_admin_bar'        => __( 'Found', 'lost_and_found' ),
        'archives'              => __( 'Item Archives', 'lost_and_found' ),
        'parent_item_colon'     => __( 'Parent Item:', 'lost_and_found' ),
        'all_items'             => __( 'All Items', 'lost_and_found' ),
        'add_new_item'          => __( 'Add New Item', 'lost_and_found' ),
        'add_new'               => __( 'Add New', 'lost_and_found' ),
        'new_item'              => __( 'New Item', 'lost_and_found' ),
        'edit_item'             => __( 'Edit Item', 'lost_and_found' ),
        'update_item'           => __( 'Update Item', 'lost_and_found' ),
        'view_item'             => __( 'View Item', 'lost_and_found' ),
        'search_items'          => __( 'Search Item', 'lost_and_found' ),
        'not_found'             => __( 'Not found', 'lost_and_found' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'lost_and_found' ),
        'featured_image'        => __( 'Featured Image', 'lost_and_found' ),
        'set_featured_image'    => __( 'Set featured image', 'lost_and_found' ),
        'remove_featured_image' => __( 'Remove featured image', 'lost_and_found' ),
        'use_featured_image'    => __( 'Use as featured image', 'lost_and_found' ),
        'insert_into_item'      => __( 'Insert into item', 'lost_and_found' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'lost_and_found' ),
        'items_list'            => __( 'Items list', 'lost_and_found' ),
        'items_list_navigation' => __( 'Items list navigation', 'lost_and_found' ),
        'filter_items_list'     => __( 'Filter items list', 'lost_and_found' ),
    );
    $args = array(
        'label'                 => __( 'Found', 'lost_and_found' ),
        'description'           => __( 'Found Post Type', 'lost_and_found' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
        'taxonomies'            => array( 'petsfound', 'electronicsfound', 'countyfound' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'rewrite'               => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'found', $args );
}
add_action( 'init', 'found_custom_post_type', 5 );
}
function found_taxonomies_pets() {
    $labels = array(
        'name'              => _x( 'Pets', 'taxonomy general name' ),
        'singular_name'     => _x( 'Pet', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Pets' ),
        'all_items'         => __( 'All Pets' ),
        'parent_item'       => __( 'Parent Pets' ),
        'parent_item_colon' => __( 'Parent Pet:' ),
        'edit_item'         => __( 'Edit Pet' ), 
        'update_item'       => __( 'Update Pet' ),
        'add_new_item'      => __( 'Add New Pet' ),
        'new_item_name'     => __( 'New Pet' ),
        'menu_name'         => __( 'Pets' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite'      => array( 'slug' => 'found/pets-found', 'with_front' => true ),
        'show_admin_column' => true,
        //'has_archive' => true

    );
    register_taxonomy( 'petsfound', 'found', $args );
    flush_rewrite_rules();
}
add_action( 'init', 'found_taxonomies_pets', 1 );
  • I have reset the permalinks on multiple occasions.
  • I have tested amending the hierarchical and with_front values from true to false and back again in case they had any affect.
  • I have ran print_r($wp_query); on the 404.php template and receive the following for the start of the query_vars:

    [query_vars] => Array ( [page] => 0 [found] => pets-found [post_type] => found [name] => pets-found

Not sure what else to look to, so hopefully someone can help.

Cheers

Damien

Edit - Adding the code for taxonomy-petsfound.php

<?php
/**
 * The template for displaying Pets Found Taxonomy.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package dazzling
 */

get_header(); ?>



<div class="breadcrumb" typeof="BreadcrumbList" vocab="http://schema.org/">
    <?php if(function_exists('bcn_display'))
    {
        bcn_display();
    }?>
</div>
<?php print_r($wp_query); ?>
        <section id="primary" class="content-area col-sm-12 col-md-12 <?php echo of_get_option( 'site_layout' ); ?>">
            <main id="main" class="site-main" role="main">

            <?php if ( have_posts() ) : ?>

                <header class="page-header">
                    <h1 class="page-title">
                        Found Items 2
                    </h1>
                    <?php
                        // Show an optional term description.
                        $term_description = term_description();
                        if ( ! empty( $term_description ) ) :
                            printf( '<div class="taxonomy-description">%s</div>', $term_description );
                        endif;
                    ?>
                </header><!-- .page-header -->

                <?php /* Start the Loop */ ?>
                <?php while ( have_posts() ) : the_post(); ?>

                    <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to override this in a child theme, then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        get_template_part( 'content', get_post_format() );
                    ?>

                <?php endwhile; ?>

                <?php dazzling_paging_nav(); ?>

            <?php else : ?>

                <?php get_template_part( 'content', 'none' ); ?>

            <?php endif; ?>

            </main><!-- #main -->
        </section><!-- #primary -->


<?php get_footer(); ?>
2
not sure if this has any bearing but you are registering the taxonomy to the post type before the post type has been registered.inarilo
How so? Is that the priortiy that is placed in this part? add_action( 'init', 'found_taxonomies_pets', 1 );?damienoneill2001
yes. and if different functions are registered with the same priority, they execute in the order they were added.inarilo
Thanks for the tip. I have changed the priorities (so the CPT uses '1' and the taxonomy uses '5'), have refreshed my permalinks but the issue still remains of the 404 page still loading.damienoneill2001
can you please add the code for taxonomy-petsfound.php?inarilo

2 Answers

3
votes

When it comes to the rewrite rules for post types and taxonomies, sometimes other rules match before the one you prefer. Maybe from a post or page slug. Even if it's one in the trash.

Try reviewing all your rewrite rules and seeing what is matching your URL request. You might need to change the order in which you create your post types or taxonomies. Try registering your taxonomies before your post types.

You can see the matching rewrite rules with this snippet:

function debug_rewrite_rules() {
    global $wp, $template, $wp_rewrite;

    echo '<pre>';
    echo 'Request: ';
    echo empty($wp->request) ? "None" : esc_html($wp->request) . PHP_EOL;
    echo 'Matched Rewrite Rule: ';
    echo empty($wp->matched_rule) ? None : esc_html($wp->matched_rule) . PHP_EOL;
    echo 'Matched Rewrite Query: ';
    echo empty($wp->matched_query) ? "None" : esc_html($wp->matched_query) . PHP_EOL;
    echo 'Loaded Template: ';
    echo basename($template);
    echo '</pre>' . PHP_EOL;

    echo '<pre>';
    print_r($wp_rewrite->rules);
    echo '</pre>';
}

add_action( 'wp_head', 'debug_rewrite_rules' );
0
votes

As far as I know if you add custom taxonomies you can access the list of products or anything you want with the help of passing parameters with that like in above if you access the list you have to use the parameter like the below url :

https://example.com/found/pets-found/dog This will give you the result because here dog is the parameters and you got the dog list.

or

$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'custom_taxonomy',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );

 $loop = new WP_Query($args);
 if($loop->have_posts()) {
    echo '<h2>'.$custom_term->name.'</h2>';

    while($loop->have_posts()) : $loop->the_post();
        echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
    endwhile;
 }
}

Hope this will help you