0

I have two Custom Post Types in my plugin and have a custom single-{cpt}.php file for each in my theme. Each page grabs the content from a specific wordpress post. The first 'house' post works but the second 'race' url gets a 404 message and then returns that default home page.

The working url is

http://bhaa.ie/house/esb

The broken url is

http://bhaa.ie/race/aib-nui-2w-2014/

The code for the working CPT is

function bhaa_register_cpt_house() {
    $houseLabels = array(
        'name' => _x( 'Houses', 'house' ),
        'singular_name' => _x( 'House', 'house' ),
        'add_new' => _x( 'Add New', 'house' ),
        'add_new_item' => _x( 'Add New House', 'house' ),
        'edit_item' => _x( 'Edit House', 'house' ),
        'new_item' => _x( 'New House', 'house' ),
        'view_item' => _x( 'View House', 'house' ),
        'search_items' => _x( 'Search Houses', 'house' ),
        'not_found' => _x( 'No houses found', 'house' ),
        'not_found_in_trash' => _x( 'No houses found in Trash', 'house' ),
        'parent_item_colon' => _x( 'Parent House:', 'house' ),
        'menu_name' => _x( 'BHAA Houses', 'house' ),
    );

    $houseArgs = array(
        'labels' => $houseLabels,
        'hierarchical' => true,
        'description' => 'BHAA House Details',
        'supports' => array('title','editor','excerpt','thumbnail','comments'),// add 'page-attributes' for parent hierarchy
        'taxonomies' => array('sector','category'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );
    register_post_type( 'house', $houseArgs );
}

The single-house.php has the following code

<div class='container_wrap main_color <?php avia_layout_class( 'main' ); ?>'>
    <div class='container template-blog template-single-blog '>
        <div class='content units <?php avia_layout_class( 'content' ); ?>'>

        <?php
        /* Run the loop to output the posts.
        * If you want to overload this in a child theme then include a file
        * called loop-index.php and that will be used instead.
        *
        */
        $page = get_page_by_title('house');
        $content = apply_filters('the_content', $page->post_content);
        echo $content;

And the race CPT is

public function bhaa_register_race_cpt() {
    $raceLabels = array(
        'name' => _x( 'Races', 'race' ),
        'singular_name' => _x( 'Race', 'race' ),
        'add_new' => _x( 'Add New', 'race' ),
        'add_new_item' => _x( 'Add New Race', 'race' ),
        'edit_item' => _x( 'Edit race', 'race' ),
        'new_item' => _x( 'New race', 'race' ),
        'view_item' => _x( 'View race', 'race' ),
        'search_items' => _x( 'Search races', 'race' ),
        'not_found' => _x( 'No races found', 'race' ),
        'not_found_in_trash' => _x( 'No races found in Trash', 'race' ),
        'parent_item_colon' => _x( 'Parent event:', 'event' ),
        'menu_name' => _x( 'BHAA Races', 'race' ),
    );

    $raceArgs = array(
        'labels' => $raceLabels,
        'hierarchical' => true,
        'description' => 'BHAA Race',
        'supports' => array('title','excerpt','editor'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'show_in_nav_menus' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'publicly_queryable' => true,
        'rewrite' => true, // array('slug' => 'race','with_front' => false),
        'capability_type' => 'post'
    );
    register_post_type( 'race', $raceArgs );
}

The single-race.php follows the same pattern

<div class='container_wrap main_color <?php avia_layout_class( 'main' ); ?>'>
    <div class='container template-blog template-single-blog '>
        <div class='content units <?php avia_layout_class( 'content' ); ?>'>

        <?php
        /* Run the loop to output the posts.
        * If you want to overload this in a child theme then include a file
        * called loop-index.php and that will be used instead.
        *
        */

        global $wp_query;
        var_dump($wp_query->query_vars);

        $page = get_page_by_title('race-page-template');
        error_log($page);

        $content = apply_filters('the_content', $page->post_content);
        error_log($content);

My 'Permalink Settings' are 'Post name http://bhaa.ie/sample-post/' and i've deleted and recreated the 'rewrite_rules' option settings in the DB with no effect.

I've included this snippet of the 'rewrite_rules' settings which show that the 'race' post_type does appear in the configuration.

s:7:"race/?$";s:24:"index.php?post_type=race";s:37:"race/feed/(feed|rdf|rss|rss2|atom)/?$";
s:41:"index.php?post_type=race&feed=$matches[1]";s:32:"race/(feed|rdf|rss|rss2|atom)/?$";
s:41:"index.php?post_type=race&feed=$matches[1]";s:24:"race/page/([0-9]{1,})/?$";

Can anybody suggest how i can determine why the second CPT URL requests are not being routed correctly.

EDIT

I installed the monkeyman-rewrite-analyzer plugin and tested the two URLs. It seems that the rewrite matches are as expected for both

Test URL:   http://bhaa.ie/house/esb

Pattern                     Substitution
house/(.+?)(/[0-9]+)?/?$    house: esb
                            page: 

(.?.+?)(/[0-9]+)?/?$        pagename: house/esb
                            page: 

[^/]+/([^/]+)/?$            attachment: esb


Test URL:   http://bhaa.ie/race/aib-nui-2w-2014/

Pattern                     Substitution
race/(.+?)(/[0-9]+)?/?$     race: aib-nui-2w-2014
                            page: 

(.?.+?)(/[0-9]+)?/?$        pagename: race/aib-nui-2w-2014
                            page: 

[^/]+/([^/]+)/?$            attachment: aib-nui-2w-2014
  • 1
    Inspect $wp_query in the template and see what the query vars look like. – Milo Nov 3 '14 at 14:44
  • I added var_dump($wp_query) to both pages, but nothing was displayed when i hit the race page. It seems to me that the single-race.php file is not getting called in the first place. – emeraldjava Nov 3 '14 at 15:00
  • 1
    if it's a 404, then your template is definitely not loading, you'll have to inspect the query in whatever template is loading to see why it's a 404. – Milo Nov 3 '14 at 15:50
  • i'm not seeing a 404 when i use the developer tools in my browser. I suspect that the moment, that the changes i made to the race cpt are not getting picked up when the rewrite settings are updated. – emeraldjava Nov 3 '14 at 16:17
  • 1
    do you register both of your post types on the init action? again, if you inspect $wp_query in whatever template loads for those requests, the problem should be apparent by how the query vars are populated. – Milo Nov 3 '14 at 16:21

Your Answer

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

Browse other questions tagged or ask your own question.