Support » Fixing WordPress » 404 Error on taxonomy's archive page

  • Resolved Neroth

    (@neroth)


    Hi, I try to make an archive page for my taxonomy “documents” (url : http://example.com/documents/) and I only get 404 errors.

    <?php
    add_action( 'init', function()
    {
    	register_post_type( 'document', array(
    		'label' => __('Documents'),
    		'singular_label' => __('Document'),
    		'public' => true,
    		'show_ui' => true,
    		'capability_type' => 'page',
    		'hierarchical' => false,
    		'supports' => array('title', 'editor', 'thumbnail', 'revisions')
    	) );
    
    	register_taxonomy( 'documents', 'document', array(
    	'hierarchical' => false,
    	'label' => 'Catégories',
    	'query_var' => true,
    	'rewrite' => array(
    		'slug' => 'documents',
    		'with_front' => false )
    	) );
    }, 0 );
    ?>
    • I’ve flush permalinks with the permalink page (Settings) in wp-admin each time I changed the rewrite array.
    • taxonomy.php exist in my theme.
    • Archive page for terms in my taxonomy work.
    • I tried several solutions I found on forums.

    http://example.com/ [work]
    http://example.com/documents [404]
    http://example.com/documents/term-example [work]

    Thanks !

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Hi Neroth

    There is no archive page for the taxonomy itself, only archive pages for the terms of a taxonomy.

    You can create a “Page” with the title (slug) “documents” and do a custom query for the taxonomy posts in the page template itself.
    https://codex.wordpress.org/Function_Reference/WP_Query
    https://developer.wordpress.org/themes/template-files-section/page-template-files/page-templates/

    Moderator keesiemeijer

    (@keesiemeijer)

    To query all posts in a taxonomy put this in your (child) theme’s functions.php file

    // Add a new public query variable: taxonomy_archive
    add_filter ( 'query_vars', 'add_taxonomy_query_var' );
    
    function add_taxonomy_query_var( $query_vars ) {
    	$query_vars['taxonomy_archive'] = '';
    	return $query_vars;
    }
    
    add_action( 'posts_clauses', 'taxonomy_archive_clauses', 10, 2 );
    
    function taxonomy_archive_clauses( $pieces, $query ) {
    	global $wpdb;
    
    	$archive_query = $query->get( 'taxonomy_archive' );
    
    	// Check if it's a query for a taxonomy archive
    	if ( !empty( $archive_query ) ) {
    
    		// Query for the taxonomy provided in the taxonomy_archive query variable
    		$pieces['join'] .= " INNER JOIN {$wpdb->term_relationships} tr_tarchive ON ($wpdb->posts.ID = tr_tarchive.object_id)";
    		$pieces['join'] .= " INNER JOIN {$wpdb->term_taxonomy} tt_tarchive ON (tr_tarchive.term_taxonomy_id = tt_tarchive.term_taxonomy_id)";
    		$pieces['where'] .= $wpdb->prepare( " AND tt_tarchive.taxonomy = %s", $archive_query );
    	}
    
    	return $pieces;
    }

    With this new code you can now use the new query var “taxonomy_archive” in your custom query inside a page template file.

    Example of the query in a simplified page template file

    <?php
    $args = array(
    	'post_type' => array( 'document' ),
    	'taxonomy_archive' => 'documents',
    );
    
    // the query
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    	<!-- the loop -->
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<!-- Loop stuff here -->
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    	<?php wp_reset_postdata(); ?>
    <?php else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    Thread Starter Neroth

    (@neroth)

    Hi keesiemeijer !
    I thought I made a mistake !

    I will use your code, thanks for your help and work !

    Have a good day !

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome 🙂

    Parse error: syntax error, unexpected ‘<‘ in C:\xampp\htdocs\my files\wordpress\wp-includes\taxonomy.php on line 387
    what i do?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘404 Error on taxonomy's archive page’ is closed to new replies.