Johnny
Johnny

Reputation: 576

Custom Taxonomy Links For Custom Post Types Not Working

I have set up a custom post type and a custom taxonomy. Then I am displaying the list of taxonomies as a set of links so that if someone clicks on that link, it should bring up all the posts under that taxonomy. Currently this is not working. It keeps taking me to the 404 page with the 'This is somewhat embarrassing isn't it?' message.

Code is as follows:

FUNCTIONS.PHP

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
    register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}

add_action('init', 'register_mypost_type');
function register_mypost_type() {
  register_post_type('companies',array(
    'labels' => array(
      'name' => 'Companies',
      'singular_name' => 'Company',
      'add_new' => 'Add New Company',
      'add_new_item' => 'Add New Company',
      'edit_item' => 'Edit Company',
      'new_item' => 'Add New Company',
      'view_item' => 'View Company',
      'search_items' => 'Search Companies',
      'not_found' => 'No companies found',
      'not_found_in_trash' => 'No companies found in trash'
    ),   
    'public' => true,
    'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt'),
    'capability_type' => 'post',
    'rewrite' => array('slug' => 'companies'),
    'taxonomies' => array('category'),
    'menu_position' => 7,
    'has_archive' => true,
    'hierarchical' => false
  ));
 }

Then on another page called 'page-company.php' I use the following code to output the list of taxonomies as links:

<?php
$args = array( 'taxonomy' => 'companies' );
wp_list_categories( $args );
?>

When I hover over one of these links the URL is displayed as:

'http://localhost:81/?companies=graphic-design'

Graphic Design being one of the categories I have added to my custom taxonomy.

However clicking this link always takes me to the 404 page.

I have set up an archives page called archive-companies.php and I thought all of this would do the trick.

Any help that anyone can provide would be greatly appreciated.

Thanks in advance.

Upvotes: 0

Views: 8143

Answers (4)

Eneas Gesing
Eneas Gesing

Reputation: 533

In your WP-Admin go to Settings and Permalinks. Don't change anything, just click Save Changes and the custom taxonomy page will work.

Upvotes: 0

Aamer Shahzad
Aamer Shahzad

Reputation: 2957

First of all create taxonomy-companies.php template in the root directory of your theme. This template will be responsible for displaying your taxonomy term posts.

then on that template you need to use the get_queried_object() to get all the taxonomy details.

e.g;

$queries_obj = get_queried_object();
echo '<pre>';
print_r( $queries_obj );
echo '</pre>';

it will return

WP_Term Object
(
    [term_id] => 10
    [name] => Featured companies
    [slug] => featured-companies
    [term_group] => 0
    [term_taxonomy_id] => 10
    [taxonomy] => companies-category
    [description] => 
    [parent] => 0
    [count] => 2
    [filter] => raw
)

then query posts like below.

$q = new WP_Query( array(
    'post_type' =>  'companies', // post type name
    'posts_per_page' =>  get_option( 'posts_per_page' ),
    'tax_query' =>  array(
        array(
            'taxonomy'  => $queries_obj->taxonomy,
            'field' => 'term_id',
            'terms' =>  array( $queries_obj->term_id )
        )
    )
) );
if ( $q->have_posts() ) :
    while ( $q->have_posts() ) :
        $q->the_post();

        // loop do stuf
        the_title();

    endwhile;

    wp_reset_query();

endif;

Upvotes: 0

Marcel
Marcel

Reputation: 21

OMG OMG OMG ... after days reading posts on how to solve the issue, using rewrite rules and ussing permalinks rewriting code, your solution was the only one that worked perfectly! The only change I needed to apply was in the custom taxonomy declaration:

This code

         'rewrite' => array(
                'slug' => 'pubs/type',
                'with_front' => false
             ),

for this code

            'rewrite' => true,

and that was it. Working like a charm!

Upvotes: 2

David Chase
David Chase

Reputation: 2073

Note:

Prior to my re-writing I tested your code and also got a 404.

1) I re-wrote you your custom post type and used your custom companies category.

2) Then I cycled from default to /%postname%/ and it works.

Functions.php

Here you go the custom post type:

// Register Custom Post Type
function register_mypost_type() {
    $labels = array(
        'name'                => _x( 'Companies', 'Post Type General Name' ),
        'singular_name'       => _x( 'Company', 'Post Type Singular Name' ),
        'menu_name'           => __( 'Company' ),
        'parent_item_colon'   => __( 'Parent Company'),
        'all_items'           => __( 'All Companies'),
        'view_item'           => __( 'View Company'),
        'add_new_item'        => __( 'Add New Company'),
        'add_new'             => __( 'New Company'),
        'edit_item'           => __( 'Edit Company'),
        'update_item'         => __( 'Update Company' ),
        'search_items'        => __( 'Search companies' ),
        'not_found'           => __( 'No companies found' ),
        'not_found_in_trash'  => __( 'No companies found in Trash'),
    );

    $rewrite = array(
        'slug'                => 'company',
        'with_front'          => true,
        'pages'               => true,
        'feeds'               => true,
    );

    $args = array(
        'label'               => __( 'company'),
        'description'         => __( 'Companies Posts' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', ),
        'taxonomies'          => array( 'companies' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 100,
        'menu_icon'           => '',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'query_var'           => 'company',
        'rewrite'             => $rewrite,
        'capability_type'     => 'post',
    );

    register_post_type( 'company', $args );
}


add_action( 'init', 'register_mypost_type', 0 );

Your custom category

add_action( 'init', 'build_taxonomies', 0 );

function build_taxonomies() {
    register_taxonomy( 'companies', 'companies', array( 'hierarchical' => true, 'label' => 'Company Categories', 'query_var' => true, 'rewrite' => true ) );
}

Upvotes: 0

Related Questions