1

I have the following code to register a custom post type and taxonomy for it:

register_post_type( 'gs_business_listings',
    array(
      'labels' => array(
        'name' => __( 'Local Businesses' ),
        'singular_name' => __( 'Local Businesses' )
      ),
      'public' => true,
            'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
            'register_meta_box_cb' => array($this,'add_meta_boxes'),
            'rewrite' => array('slug'=> '%larger-region%/%regions%')
        )
  );

    register_taxonomy(
        'gs_business_region_harvey',
        'gs_business_listings',
        array(
            'labels' => array(
                'name' => 'Harvey County',
                'singular_name' => 'Region',
            ),
            'rewrite' => array( 'slug' => 'harvey-county', 'hierarchical' => true ),
            'hierarchical' => true,
            'has_archive' => true,
        )
    );

I would expect that accessing www.example.com/harvey-county would show an archive of all posts that use the custom taxonomy. But I get a 404 not found. What do I have wrong? Thanks.

Note:currently if I access www.example.com/harvey-county/newton it shows the archive page for the newton term correctly.

2
  • 1
    There's nothing wrong- there is no general taxonomy archive, only individual terms.
    – Milo
    Jan 10, 2017 at 16:26
  • Ok. Do you have any suggestions as to how to implement something that would actually behave that way? I'm not where I can test this, but would a page-harvey-county.php in the theme with a loop to display all posts that include any terms under that taxonomy work? Jan 10, 2017 at 17:10

1 Answer 1

1

As Milo said, there is no general taxonomy archive. But it made sense to have one in my situation. Here is what I did to imitate a general archive:

I created a blank page in Wordpress Admin name Harvey County with a harvey-county slug. Then I added a page-harvey-county.php template file to my theme. I copied the code from my archive.php. I tweaked the loop portion to look like this:

    <?php 
    $taxonomy_terms = get_terms( 'gs_business_region_harvey', array(
        'hide_empty' => 0,
        'fields' => 'ids'
    ) );
    $args = array(
       'post_type' => 'gs_business_listings',
       'tax_query' => array(
                        array(
                          'taxonomy' => 'gs_business_region_harvey',
                          'terms' => $taxonomy_terms
                          ),
                      ),
        );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) : ?>

                //header code 

                <?php
                // Start the Loop.
                $addressWrapper = array();
                $addresses = array();
                while ( $query->have_posts() ) : $query->the_post();

                 //Call get_template_part and such.
                 endwhile;

        endif; ?>

This works, but bothers me a little as it's not very DRY, copying archive.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.