Having issues with permalink generation for a custom post type.

THe following code works fine for viewing my individual properties.

This is the permalink structure
/listings/%type%/%title%/

My issue is that the archive URL is now a 404 not found.

/listings/

Any ideas what I need to do to fix the archive?

function create_real_estate_property() {
register_post_type( 'properties',
    array(
        'labels' => array(
            'name' => 'Properties',
            'singular_name' => 'Property',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Property',
            'edit' => 'Edit',
            'edit_item' => 'Edit Property',
            'new_item' => 'New Property',
            'view' => 'View',
            'view_item' => 'View Property',
            'search_items' => 'Search Properties',
            'not_found' => 'No Property found',
            'not_found_in_trash' => 'No Properties found in Trash',
            'parent' => 'Parent Property'
        ),
        'public' => true,
        'rewrite' => array(
            'slug'=>'listings/%type%',
            'with_front' => true,
            'hierarchical' => true
        ),
        //'query_var' => true,
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
        //'taxonomies' => array( 'category' ),
        'has_archive' => true,
        'hierarchical'  => true
    )
);
flush_rewrite_rules();

}

function my_taxonomies_product() {
$labels = array(
    'name' => _x( 'Types', 'taxonomy general name' ) ,
    'singular_name' => _x( 'Type', 'taxonomy singular name' ) ,
    'search_items' => __( 'Search Types' ) ,
    'all_items' => __( 'All Types' ) ,
    'parent_item' => __( 'Parent Type' ) ,
    'parent_item_colon' => __( 'Parent Type:' ) ,
    'edit_item' => __( 'Edit Type' ) ,
    'update_item' => __( 'Update Type' ) ,
    'add_new_item' => __( 'Add New Type' ) ,
    'new_item_name' => __( 'New Type Name' ) ,
    'menu_name' => __( 'Type' ) ,
);
$argsTax = array(
    'labels' => $labels,
    'hierarchical'  => true,
    'public'        => true,
    'query_var'     => 'type',
    //slug prodotto deve coincidere con il primo parametro dello slug del Custom Post Type correlato
    'rewrite'       =>  array('slug' => 'properties' ),
    '_builtin'      => false,
);
register_taxonomy( 'type', 'properties', $argsTax );

}

add_action( ‘init’, ‘my_taxonomies_product’, 0 );

add_filter(‘post_link’, ‘type_permalink’, 1, 3);
add_filter(‘post_type_link’, ‘type_permalink’, 1, 3);

function type_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, ‘%type%’) === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, 'type');
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
      $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = 'no-category';
return str_replace('%type%', $taxonomy_slug, $permalink); }

Read more here: Custom post type archive not found


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , ,

Wordpress related questions and answers: