• Resolved dwarrell

    (@dwarrell)


    I have added a couple of custom taxonomies (called Section and Column) to my theme. When registering these taxonomies, I set “rewrite” to true so that I would get category-style permalinks for my taxonomies. For example, https://www.example.com/section/section1/ would be the permalink for the archive page for the Section named Section1.

    These permalinks work fine as long as there is at least one post associated with the taxonomy term being used in the permalink. However, if there are no posts associated with the term, browsing to the archive permalink results in the 404 page. The behaviour I would have expected is that instead of a 404, I would get the archive page with the “No posts” message, as happens if you browse to the archive permalink for a Category that contains no posts.

    I used the WordPress Internal Rewrite viewer plugin to view my rewrites, and these are the rewrite rules that are in place for the “section” custom taxonomy:

    [section/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?section=$1&feed=$2
    [section/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?section=$1&feed=$2
    [section/(.+?)/page/?([0-9]{1,})/?$] => index.php?section=$1&paged=$2
    [section/(.+?)/?$] => index.php?section=$1

    As far as I can see, these are identical to the rewrite rules in place for Categories. I tried visiting the “ugly” permalink for one of the sections in question (i.e. https://www.example.com/index.php?section=section1). This displayed the archive page with the “No posts” message, as I would expect.

    So my question is – why does the “pretty” permalink not work when the “ugly” permalink it is supposedly redirecting to does work properly? Is there any way I can find out what the “pretty” permalinks are actually redirecting to (i.e. which .php template(s) are being used to process the request)? Is there something else I am missing?

    Thanks,

    David

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter dwarrell

    (@dwarrell)

    For those playing along at home: an update and (apparent) fix.

    I also noticed that the 404 was occurring when paging through the archives of my custom taxonomies. Viewing the first page of the archive worked fine, but visiting subsequent pages resulted in a 404.

    The fix involves changes to two functions in the WordPress core files.

    1) Modify handle_404 in classes.php to add an exception for Taxonomies alongside those already in place for Tags, Categories and Authors. Change the following line (classes.php line 464 in WP v2.9.1):
    if ( ( is_tag() || is_category() || is_author() ) && $wp_query->get_queried_object() ) {
    to:
    if ( ( is_tag() || is_category() || is_author() || is_tax() ) && $wp_query->get_queried_object() ) {

    2) Modify get_queried_object in query.php to return empty Taxonomy terms. Change the following line (query.php line 2563 in WP v2.9.1):
    $term = &get_terms($tax, array('slug'=>$slug));
    to:
    $term = &get_terms($tax, array('slug'=>$slug, 'hide_empty'=>0));

    If anyone has any insights into the validity or otherwise of these changes, I’d appreciate hearing them, but they seem to be working fine for me so far.

    I don’t have any insights but I wanted to confirm that this worked for me solving the same problem.

    Of course changing the core is never good but this has been the only thing that has worked so far, so thanks for this. I

    I have another site also WP 2.9.1 with a custom taxonomy and archive pages work as expected. The only difference is that it’s just one custom taxonomy. On the site that had issues I had a couple of custom taxonomies.

    Hmmm.. I just realized my problem was a Plugin conflict, specifically Event Calendar 3. This still solved the 404 but posts still weren’t displaying.

    Just to follow up if anyone find they’re in having problems listing custom taxonomies on archive.php or taxonomy.php when the Event Calendar 3.2.2 plugin is installed.

    Te fix is similar to the one above. Open the file eventcalendar3.php and modify line 161 replacing

    if($ec3->query->is_page || $ec3->query->is_single || $ec3->query->is_admin)

    with

    if($ec3->query->is_page || $ec3->query->is_tax || $ec3->query->is_single || $ec3->query->is_admin)

    This didn’t work for me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Archive permalink for custom taxonomy term gives 404’ is closed to new replies.