0

Why are my hits for a taxonomy term page getting a 404 error?

I have taxonomy "source', registered in code through a plugin...

<?php
/**
 * Plugin Name: Context Taxonomy for Sources
 * Description: Register custom taxonomy for sources.
 * Version: 1.0.0
 * Author: Robert Andrews
 */




/**
 * ==============================================================================
 *                      REGISTER TAXONOMY
 * ==============================================================================
 */

if ( ! function_exists( 'register_taxonomy_source' ) ) {

function register_taxonomy_source() {

  $labels = array(
        'name'                       => _x( 'Sources', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Source', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Sources', 'text_domain' ),
        'all_items'                  => __( 'All Sources', 'text_domain' ),
        'parent_item'                => __( 'Parent Source', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Source:', 'text_domain' ),
        'new_item_name'              => __( 'New Source Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Source', 'text_domain' ),
        'edit_item'                  => __( 'Edit Source', 'text_domain' ),
        'update_item'                => __( 'Update Source', 'text_domain' ),
        'view_item'                  => __( 'View Source', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Sources with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove Source', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular Source', 'text_domain' ),
        'search_items'               => __( 'Search Source', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No Source', 'text_domain' ),
        'items_list'                 => __( 'Sources list', 'text_domain' ),
        'items_list_navigation'      => __( 'Sources list navigation', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                       => 'source',
        'with_front'                 => true,
        'hierarchical'               => false,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
    'single_value'               => false, // Use single-select radio button, only one Source per object
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
    );
    register_taxonomy(
        /* taxonomy name */         'source',
        /* attach to object */  array( 'quote','article','report','event', 'post' ),
        /* arguments */                 $args
    );

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

}




?>

The taxonomy works in the back-end.

Now I am building out a theme, I want to see that a page like http://www.example.com/source/term-name works. But it doesn't.

My .htaccess file contained...

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>

# END WordPress

# Modified for Multisite https://www.namecheap.com/support/knowledgebase/article.aspx/9191/29/how-to-create-a-wildcard-subdomain-in-cpanel
# Previous here:
# RewriteEngine On
# RewriteBase /
# RewriteRule ^index\.php$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]

I have just removed those parts. Before this, the 404 /source/term-name page was actually getting redirected to the homepage.

I have...

  • Removed the above .htaccess portions.
  • Clicked Save on the Permalinks page
  • Added flush_rewrite_rules() and similar to my code.

Still it's coming up 404. I don't understand it.

One interesting thing to note... On my "Source" taxonomy listing in the back-end, when you open a taxonomy term page with the view link, I just saw each site URL is like http://www.example.com/blog/source/term-name - not http://www.example.com/source/term-name. It should be the latter. However, both variants fail 404.

On my Permalinks Settings page, "Common Settings" is set to "Custom Structure": /blog/%year%/%monthnum%/%day%/%postname%/ That may be where "blog" is coming from, but I still don't understand the 404.

I just removed "/blog/". Now the URL is http://www.example.com/source/term-name - but, still, that is 404.

  • 1
    I would suggest installing the query-monitor plugin to see whats happening behind the scenes. has helped me in the past. Navigate to example.com/source/term-name then open up the query-monitor tab whilst logged in (should be in the admin bar) and then go to the Request section. what does it say? – Craig Wayne Jul 12 '18 at 21:35
  • @CraigWayne Interesting... Message: "Use of undefined constant term_name - assumed 'term_name'". Count: 1. Location: wp-content/themes/elegant/taxonomy-source.php:38. Caller: wp-includes/template-loader.php wp-includes/template-loader.php:74. Component: Core. >>>>> Line 38 in taxonomy-source.php included "<?php echo term_name; ?>" - it was objecting to term_name; I'm changing to $term = get_queried_object(); followed by <?php echo $term->name; ?> Thanks for the Query Monitor tip, I guess. it's like a Chrome Inspector for WordPress. – Robert Andrews Jul 12 '18 at 22:19
  • Glad to be of help! Yup, that plugin is an essential part of my development, especially for rewrite voodoo. Anyways, all the best! – Craig Wayne Jul 13 '18 at 0:29
2

Following @CraigWayne's suggestion to use Query Monitor...

Message: "Use of undefined constant term_name - assumed 'term_name'". Count: 1. Location: wp-content/themes/elegant/taxonomy-source.php:38. Caller: wp-includes/template-loader.php wp-includes/template-loader.php:74. Component: Core. >>>>> Line 38 in taxonomy-source.php included ""

it was objecting to term_name in my taxonomy-source theme file; I changed it to $term = get_queried_object(); followed by name; ?>

The takeaway for others experiencing a 404 taxonomy term template - it might be dodgy code within the template file.

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.