1

I am trying to implement an archive for a custom post type:

http://mywebsite:8888/about-us/client-news/2015/03/

The problem is that it keeps serving me a 404 page and I cannot work out what page template its failing to find. I have tried turning on debug and a number of other steps

The registration:

function client_news() {

    $labels = array(
        'name' => _x("Client News", "post type general name"),
        'singular_name' => _x("Client News Item", "post type singular name"),
        'menu_name' => 'Client News',
        'add_new' => _x("Add New", "news item"),
        'add_new_item' => __("Add New News Item"),
        'edit_item' => __("Edit News Item"),
        'new_item' => __("New News Item"),
        'view_item' => __("View News Item"),
        'search_items' => __("Search Client News"),
        'not_found' =>  __("No Useful Items Found"),
        'not_found_in_trash' => __("No Useful Items Found in Trash"),
        'parent_item_colon' => ''
    );
    // Register post type
    register_post_type('clientnews' , array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array( 'slug' => 'news' ),
        'supports' => array('title', 'editor', 'thumbnail')
    ) );
}

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

Archive loop is as follows:

<ul class="news_archive">
    <?php
        add_filter( 'get_archives_link', 'get_archives_clientnews_link', 10, 2 );
        wp_get_archives( array( 'post_type' => 'clientnews', 'type' => 'monthly' ) );
        remove_filter( 'get_archives_link', 'get_archives_clientnews_link', 10, 2 );
    ?>
</ul>
  • 1
    have you refreshed your permalinks – alexwc_ Mar 4 '15 at 17:12
  • Yes, several times :( – Eth Mar 4 '15 at 17:22
  • Show your CPT registration and your archive loop. – alexwc_ Mar 4 '15 at 17:23
  • I have updated the description to show what you require – Eth Mar 4 '15 at 17:30
  • custom post types don't have year/month rewrite rules, your archive is just /clientnews/. – Milo Mar 4 '15 at 18:00
1

I have managed to resolve this by adding in a custom rewrite into the functions file:

// Add custom rewrite rules to handle things like years in custom post archives
function add_rewrite_rules($aRules) {
    $aNewRules = array(
        'news/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$' => 'index.php?post_type=clientnews&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
        'news/([0-9]{4})/([0-9]{2})/?$' => 'index.php?post_type=clientnews&year=$matches[1]&monthnum=$matches[2]',
        'about-us/other-news/([0-9]{4})/([0-9]{2})/page/?([0-9]{1,})/?$' => 'index.php?post_type=othernews&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
        'about-us/other-news/([0-9]{4})/([0-9]{2})/?$' => 'index.php?post_type=othernews&year=$matches[1]&monthnum=$matches[2]'
    );
    $aRules = $aNewRules + $aRules;
    return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Then in the archive page I changed the query to:

$aParts = explode( '/', $_SERVER['REQUEST_URI'] );

$iYear  = get_query_var('year');
$iMonth = get_query_var('monthnum');

if( $iMonth <= 0 && $iYear > 0 )
{
    $iMonth = $aParts[ 4 ];
}

$news = new WP_Query('showposts=6&post_type=othernews&paged='. $paged . '&year=' . $iYear . '&monthnum=' . $iMonth );
  • +1 for taking the effort to answer your own question. Be sure to accept your own answer once the initial 2 day restriction is lifted :-) – Pieter Goosen Mar 5 '15 at 11:49
0

Try visiting :

http://mywebsite:8888/about-us/news/2015/03/

You are using slug => 'news' so 'client-news' will give you an error

  • I have a couple of these post types and have copied the registration from one and the archive list from another. Basically, what I want to know is. Is there a way of finding out the template that WP is looking for BEFORE it serves the 404? – Eth Mar 5 '15 at 9:25
  • 404 is not related to template, if WordPress can find the post than it will show it. Even if template is not available than it will show it using index.php but never 404. If 404 is being shown than your post is not located by WordPress and most probable reason is 'bad permalink structure' To test it quickly, simply set your permalink to default and than visit your post. – Fiaz Husyn Mar 5 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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