0

I have two custom post types 1 called melon-parties and another called party-categories. for posts in melon-parties i create a metabox which gets populated from all posts from parties-categories. I then have been able to rewrite the url of melon-parites to www.domain.com/parties/christmas-party/thames-river-party

where christmas-party is a custom post from party-categories.

I can access that page (www.domain.com/parties/christmas-party/thames-river-party) but when i try and go to www.domain.com/parties/christmas-parties it redirects to the homepage and not the custom post which is where my problem lies. I have had to do a custom rewrite for that as well because when i use the rewrite option when i register the post type i get a 404 on the party post.

here is my code that creates all the custom post types and rewrite rules:

function register_custom_post_types() {
    $args = array(
        'labels' => array(
            'name' => __( 'Parties' ),
            'singular_name' => __( 'Party' )
        ),
        'menu_position' => 50,
        'public' => true,
        'has_archive' => 'true',
        'hierarchical' => true,
        'taxonomies' => array('post_tag'),
        'capability_type' => 'post',
        'rewrite' => false,
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-parties', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Type of Parties' ),
            'singular_name' => __( 'Type of Party' )
        ),
        'menu_position' => 50,
        'public' => true,
        'has_archive' => 'true',
        'hierarchical' => true,
        'show_ui' => true,
        'publicly_queryable' => true,
        'capability_type' => 'page',
        'supports' => array('title', 'thumbnail', 'revisions', 'page-attributes'),
        'rewrite' => false
    );

    register_post_type('party-categories', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Home Slider' ),
            'singular_name' => __( 'Home Slide' )
        ),
        'menu_position' => 50,
        'public' => false,
        'show_ui' => true,
        'hierarchical' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-home-slider', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Case Studies' ),
            'singular_name' => __( 'Case Study' )
        ),
        'menu_position' => 50,
        'public' => true,
        'hierarchical' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => array('with_front' => false, 'slug' => 'case-studies'),
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-case-studies', $args);
}
add_action( 'init', 'register_custom_post_types' );

add_action('init', 'tdd_add_rewrite_rules');

function tdd_add_rewrite_rules() {

// Register custom rewrite rules

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%melon-parties%', '([^/]+)', 'melon-parties=');
    $wp_rewrite->add_rewrite_tag('%parties_categories%', '([^/]+)', 'cmb-event_category=');

    $wp_rewrite->add_permastruct('melon-parties', '/parties/%parties_categories%/%melon-parties%', false);
}

add_filter('post_type_link', 'tdd_permalinks', 10, 3);

    function tdd_permalinks($permalink, $post, $leavename) {

    $no_data = 'no-category';

    $post_id = $post->ID;

    if($post->post_type != 'melon-parties' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))

    return $permalink;

    $var1 = get_post_meta($post_id, 'cmb-event_category', true);

    $var1 = sanitize_title($var1);

    if(!$var1) { $var1 = $no_data; }

    $permalink = str_replace('%parties_categories%', $var1, $permalink);

    return $permalink;

}

add_action('init', 'partyCategoryRewriteRules');

function partyCategoryRewriteRules() {

// Register custom rewrite rules

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%party-categories%', '([^/]+)', 'party-categories=');

    $wp_rewrite->add_permastruct('party-categories', '/parties/%category%', false);
}

add_filter('post_type_link', 'partyCategoryPermalinks', 10, 3);

    function partyCategoryPermalinks($permalink, $post, $leavename) {

    $no_data = 'no-category';

    $post_id = $post->ID;

    if($post->post_type != 'party-categories' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))

    return $permalink;

    $var1 = $post->post_name;

    $var1 = sanitize_title($var1);

    if(!$var1) { $var1 = $no_data; }

    $permalink = str_replace('%category%', $var1, $permalink);

    return $permalink;

}

Please can someone advise where i am going wrong. would really appreciate it.

1

I fixed this issue by adding a rewrite rule to my functions.php file at the top:

function melon_rewrites($rules) {
        $new_rules = array(
            'parties/([^/]*)/([^/]*)$' => 'index.php?melon-parties=$matches[2]'
        );
        $rules = $new_rules + $rules;
        return $rules;
    }
    add_filter('rewrite_rules_array', 'melon_rewrites');

I then changed my cpt to allow a query_var:

function register_custom_post_types() {
    $args = array(
        'labels' => array(
            'name' => __( 'Parties' ),
            'singular_name' => __( 'Party' )
        ),
        'menu_position' => 50,
        'public' => true,
        'has_archive' => 'true',
        'hierarchical' => true,
        'taxonomies' => array('party-tags'),
        'capability_type' => 'post',
        'query_var' => true,
        'rewrite' => false,
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-parties', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Type of Parties' ),
            'singular_name' => __( 'Type of Party' )
        ),
        'menu_position' => 50,
        'public' => true,
        'has_archive' => 'true',
        'hierarchical' => true,
        'show_ui' => true,
        'publicly_queryable' => true,
        'capability_type' => 'page',
        'query_var' => true,
        'supports' => array('title', 'thumbnail', 'revisions', 'page-attributes'),
        'rewrite' => false
    );

    register_post_type('party-categories', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Home Slider' ),
            'singular_name' => __( 'Home Slide' )
        ),
        'menu_position' => 50,
        'public' => false,
        'show_ui' => true,
        'hierarchical' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-home-slider', $args);

    $args = array(
        'labels' => array(
            'name' => __( 'Case Studies' ),
            'singular_name' => __( 'Case Study' )
        ),
        'menu_position' => 50,
        'public' => true,
        'hierarchical' => false,
        'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => array('with_front' => false, 'slug' => 'case-studies'),
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );

    register_post_type('melon-case-studies', $args);
}
add_action( 'init', 'register_custom_post_types' );

I also had to make a couple changes to my rewrite rules as i wasnt actually pointing to my cpt:

add_action('init', 'tdd_add_rewrite_rules');

    function tdd_add_rewrite_rules() {

    // Register custom rewrite rules
        add_rewrite_tag('%melon-parties%', '([^/]+)');
        add_rewrite_tag('%party-categories%', '([^/]+)');

        add_permastruct('melon-parties', 'parties/%party-categories%/%melon-parties%', false);
    }

    add_filter('post_type_link', 'tdd_permalinks', 10, 3);

        function tdd_permalinks($permalink, $post, $leavename) {

        $no_data = 'no-category';

        $post_id = $post->ID;

        if($post->post_type != 'melon-parties' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))

        return $permalink;

        $var1 = get_post_meta($post_id, 'cmb-event_category', true);

        $var1 = get_the_title($var1);

        $var1 = sanitize_title($var1);

        if(!$var1) { $var1 = $no_data; }

        $permalink = str_replace('%party-categories%', $var1, $permalink);

        return $permalink;

    }

    add_action('init', 'partyCategoryRewriteRules');

    function partyCategoryRewriteRules() {

    // Register custom rewrite rules

        global $wp_rewrite;
        $wp_rewrite->add_rewrite_tag('%party-categories%', '([^/]+)', 'party-categories=');

        $wp_rewrite->add_permastruct('party-categories', '/parties/%party-categories%', false);
    }

    add_filter('post_type_link', 'partyCategoryPermalinks', 10, 3);

        function partyCategoryPermalinks($permalink, $post, $leavename) {

        $no_data = 'no-category';

        $post_id = $post->ID;

        if($post->post_type != 'party-categories' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))

        return $permalink;

        $var1 = $post->post_name;

        $var1 = sanitize_title($var1);

        if(!$var1) { $var1 = $no_data; }

        $permalink = str_replace('%party-categories%', $var1, $permalink);

        return $permalink;

    }

Now my url structure is correct

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.