0

I have a custom post type, "toys", with a custom taxonomy, "toy_series". I wanted to get the custom taxonomy in the permalink:

domain/toys/toy_series/toy-name

I got that working just fine by changing the rewrite on the post type to:

/toys/%toy_series%

and editing the link structure with post_type_link in my functions.php:

function append_query_string( $link, $post ) {
    if ( 'toys' == get_post_type( $post ) ) {
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'toy_series');   
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'no-series';

        return str_replace('%toy_series%', $taxonomy_slug, $link);
    }
    return $link;
}
add_filter( 'post_type_link', 'append_query_string', 10, 2 );

However, I then discovered the link to the toys archive was failing because it was trying to go to the new toys rewrite. The link in the menu was domain/toys/%toy_series% Made sense. So, I changed the rewrite for the archive itself to be /toys/

That made the link in the menu correct again, but now I get a 404 on the archive page. I have a custom template named archive-toys.php, but WP doesn't seem to want to use that. It's defaulting back to index.php (I have not created a 404.php yet) and not outputting anything.

What have I missed?

Thanks!


As requested, I am adding in the code for the CPT and taxonomy. I am using the CPT UI plugin, so I don't really have control over the order.

    $labels = array(
        "name" => "Toys",
        "singular_name" => "Toy",
        "menu_name" => "Toys",
        "all_items" => "All Toys",
        "add_new" => "Add New",
        "add_new_item" => "Add New Toy",
        "edit" => "Edit",
        "edit_item" => "Edit Toy",
        "new_item" => "New Toy",
        "view" => "View",
        "view_item" => "View Toy",
        "search_items" => "Search Toys",
        "not_found" => "No Toys Found",
        "not_found_in_trash" => "No Toys Found in Trash",
        "parent" => "Parent Toy",
        );

    $args = array(
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "show_ui" => true,
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "/toys/%toy_series%", "with_front" => true ),
        "query_var" => true,
                        "supports" => array( "title", "revisions" ),        "taxonomies" => array( "toy_series", "factions" )   );
    register_post_type( "toys", $args );


$labels = array(
        "name" => "toy_series",
        "label" => "Toy Series",
            );

    $args = array(
        "labels" => $labels,
        "hierarchical" => true,
        "label" => "Toy Series",
        "show_ui" => true,
        "query_var" => true,
        "rewrite" => array( 'slug' => 'toy_series', 'with_front' => true ),
        "show_admin_column" => false,
    );
    register_taxonomy( "toy_series", array( "toys" ), $args );
5
  • please add the code you use to register the taxonomy and post type. also note that the order you register things in may matter.
    – Milo
    Commented Mar 17, 2015 at 15:56
  • Added the code for the CPT and taxonomy. I am using the CPT UI plugin, so I don't have control over the order that I know of.
    – Mattaton
    Commented Mar 17, 2015 at 16:24
  • your register_post_type is missing the has_archive parameter. if you set that to toys and flush permalinks, it should work fine.
    – Milo
    Commented Mar 17, 2015 at 19:14
  • The $args for register_post_type shows "has_archive" => true. Is that what you mean? In CPT UI, it is set to true and in the blank under it, I have the archive slug set to /toys/. The only thing I don't see reflected in CPT UI's code output is that archive slug. But, when I changed that in the CPT UI admin, it did change the archive link in the main menu for Toys. So, I know it's doing something.
    – Mattaton
    Commented Mar 17, 2015 at 19:44
  • Ah, I see. I had it set to /toys/. It needed to be set to just toys. Now it works! Thanks!
    – Mattaton
    Commented Mar 17, 2015 at 20:04

0

Your Answer

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