0

There will be a lot of code blocks below, but here's the short version:

On a site, I have a Resources custom post type, with a Resource Types taxonomy.

The URL for a Resource Type post archive is: http://www.example.com/resources/articles/

The URL for a Resource post, with the Articles taxonomy term checked is: http://www.example.com/resources/articles/[MY-POST-TITLE-HERE]/

This is all working great now.

The blog landing page (set in the Reading settings) is a page called "blog" that is a child of a page called "resources". I have my permalink settings set to Custom Structure with /resources/blog/%postname%/ in the field.

When I go to the Blog list page, I go to the desired URL (http://www.example.com/resources/blog/) but I get a 404 page, and if I view a blog post I also get the desired URL (http://www.example.com/resources/blog/[MY-POST-TITLE-HERE]/) but I also get a 404. Category pages return the correct URL, without a 404.

I'm unsure how to resolve this.

Here is all of the relevant code that pertains to URL rewriting for the Resources section of the site:

Register Resource Type Taxonomy

//Register Resource Type Taxonomy
add_action( 'init', 'register_taxonomy_resource_types' );

function register_taxonomy_resource_types() {
    $resource_slug = "/" . get_page_uri( constant('resourceLandingID') );
    $labels = array( 
        'name' => _x( 'Resource Types', 'resource_types' ),
        'singular_name' => _x( 'Resource Type', 'resource_types' ),
        'search_items' => _x( 'Search Resource Types', 'resource_types' ),
        'popular_items' => _x( 'Popular Resource Types', 'resource_types' ),
        'all_items' => _x( 'All Resource Types', 'resource_types' ),
        'parent_item' => _x( 'Parent Resource Type', 'resource_types' ),
        'parent_item_colon' => _x( 'Parent Resource Type:', 'resource_types' ),
        'edit_item' => _x( 'Edit Resource Type', 'resource_types' ),
        'update_item' => _x( 'Update Resource Type', 'resource_types' ),
        'add_new_item' => _x( 'Add New Resource Type', 'resource_types' ),
        'new_item_name' => _x( 'New Resource Type', 'resource_types' ),
        'separate_items_with_commas' => _x( 'Separate resource types with commas', 'resource_types' ),
        'add_or_remove_items' => _x( 'Add or remove resource types', 'resource_types' ),
        'choose_from_most_used' => _x( 'Choose from the most used resource types', 'resource_types' ),
        'menu_name' => _x( 'Resource Types', 'resource_types' ),
    );
    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => false,
        'show_admin_column' => false,
        'hierarchical' => true,
        'rewrite' => array(
            'slug' => '/resources',
            'with_front' => false
        ),
        'query_var' => true
    );
    register_taxonomy( 'resource_types', array('resources'), $args );
}

Register Resource Custom Post Type

//Register Resources post type
add_action( 'init', 'register_cpt_resources' );
//$resource_slug = "/" . get_page_uri( constant('resourceLandingID') );
function register_cpt_resources() {
    $labels = array( 
        'name' => _x( 'Resources', 'resources' ),
        'singular_name' => _x( 'Resources', 'resources' ),
        'add_new' => _x( 'Add New', 'resources' ),
        'add_new_item' => _x( 'Add New Resources', 'resources' ),
        'edit_item' => _x( 'Edit Resources', 'resources' ),
        'new_item' => _x( 'New Resources', 'resources' ),
        'view_item' => _x( 'View Resources', 'resources' ),
        'search_items' => _x( 'Search Resources', 'resources' ),
        'not_found' => _x( 'No resources found', 'resources' ),
        'not_found_in_trash' => _x( 'No resources found in Trash', 'resources' ),
        'parent_item_colon' => _x( 'Parent Resources:', 'resources' ),
        'menu_name' => _x( 'Resources', 'resources' ),
    );
    $args = array( 
        'labels' => $labels,
        'hierarchical' => false,
        'supports' => array( 'title', 'editor', 'excerpt' ),
        'taxonomies' => array( 'resource_types', 'resource_tags' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 20,        
        'show_in_nav_menus' => false,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => false,
        'query_var' => true,
        'can_export' => true,
        'rewrite'   => array( 
            'slug' => '/resources/%resource_types%',
            'with_front' => false
        ),
        'capability_type' => 'post'
    );
    register_post_type( 'resources', $args );
}

In my functions file, I have this function that runs to add Resource Type term to the permalink of the Resource post type posts.

Resource Type Term added to Permalink

add_filter('post_type_link', 'projectcategory_permalink_structure', 10, 4);
function projectcategory_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%resource_types%')) {
        $projectscategory_type_term = get_the_terms($post->ID, 'resource_types');
        if (!empty($projectscategory_type_term))
            $post_link = str_replace('%resource_types%', array_pop($projectscategory_type_term)->
        slug, $post_link);
        else
            $post_link = str_replace('%resource_types%', 'uncategorized', $post_link);
    }
    return $post_link;
}

So again, the entire Resources section works great. It's just the Blog list page (child of Resources) and the individual blog posts that return a 404.

5
  • please have look : wordpress.stackexchange.com/questions/202859/…
    – jas
    Sep 17, 2015 at 14:56
  • Nothing in that answer helps. The Custom Post Type's has_archive is already set to true, and adding the flush rewrite function had no effect on how the Blog and Blog Posts are being displayed.
    – Joe
    Sep 17, 2015 at 15:00
  • Welcome to the nightmare that is permalinks. Unless something has changed recently, this is not possible. The issue is that the Blog is a page and when you try to visit it via the URL you want, WordPress is not looking for a page, it's looking for a resources post. It doesn't bother checking for a page. My only recommendation would be to create a taxonomy or taxonomy term (depending on your setup) for blog under resources and simply put blog entries under resources, yes this sucks.
    – deflime
    Sep 17, 2015 at 19:59
  • The other, which is what I normally do in this situation is to simply call the post type 'resource' instead of 'resources' and create the my own index template for 'resource' posts on the Resources page. This allows you to have both the posts and pages separate. The url to an article then changes slightly, but personally this doesn't bother me, it's still clear enough to users. This is not ideal but I don't believe it is possible if you want pages underneath a custom post type with a matching slug.
    – deflime
    Sep 17, 2015 at 20:04
  • I actually did the second item you mentioned, in order to get the site working again for client review. I just changed the permalink to the Resources to "resource"
    – Joe
    Sep 17, 2015 at 20:07

0

Your Answer

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

Browse other questions tagged or ask your own question.