add rewrite rule custom post type

I’ve been working on this for 4 days now and can’t figure out what I’m missing. I have searched and tried everything that comes up for wp rewriting.

It works but doesn’t save. So if I go to quick edit and save, click view it works. If I refresh and hover over view %property_type% is missing and doesn’t work.

Read More

Any Idea what I’m missing?

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite'        => array(
    'slug'       => str_replace( basename( home_url() ), '', 'investment-property' ) . '/%property_type%',
    'with_front' => false
),
'capability_type' => 'page',
'has_archive'   => true,
'hierarchical'  => true,
'comment_status'=> 'closed',
'exclude_from_search' =>false,
'supports' => array('title', 'editor', 'thumbnail'),

);

register_post_type( 'property', $args );

I also have this…

function my_flush_rules() {
$rules = get_option( 'rewrite_rules' );

if ( ! isset( $rules['(investment-property/%property_type%/)/(.+)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}

}

…and this

function icn_property_type_link( $permalink, $post, $leavename ) {
global $wp_query, $wpsc_page_titles, $wp_rewrite, $wp_current_filter;
$rewritecode = array(
    '%property_type%',
    $leavename ? '' : '%postname%',
);
if ( is_object( $post ) ) {
    // In wordpress 2.9 we got a post object
    $post_id = $post->ID;
} else {
    // In wordpress 3.0 we get a post ID
    $post_id = $post;
    $post = get_post( $post_id );
}

// Only applies to ICN properties, don't stop on permalinks of other CPTs
if ($post->post_type != 'property')
    return $permalink;

$permalink_structure = get_option( 'permalink_structure' );

// This may become customize-able later
$our_permalink_structure = str_replace( basename( home_url() ), '', 'investment-property' ) . "/%property_type%/%postname%/";
// Mostly the same conditions used for posts, but restricted to items with a post type of "wpsc-product "

if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
    $property_categories = get_the_terms($post_id, 'property_type' );


    $property_category_slugs = array( );
    foreach ( $property_categories as $property_category ) {
            if($property_category->slug == 'featured'){continue;}
        $property_category_slugs[] = $property_category->slug;
    }
    // If the property is associated with multiple categories, determine which one to pick

    if ( count( $property_categories ) == 0 || $property_categories == '') {
        $category_slug = 'real-estate-for-sale';
    } elseif ( count( $property_categories ) > 1 ) {
        if ( (isset( $wp_query->query_vars['property'] ) && $wp_query->query_vars['property'] != null) && in_array( $wp_query->query_vars['property'], $property_category_slugs ) ) {
            $property_category = $wp_query->query_vars['property'];
        } else {
            $link = $property_categories[0]->slug;
            if ( ! in_array( 'wp_head', $wp_current_filter) && isset( $wp_query->query_vars['property_type'] ) ) {
                $current_cat = $wp_query->query_vars['property_type'];
                if ( in_array( $current_cat, $property_category_slugs ) ){
                    $link = $current_cat;
                }
            }

            $property_category = $link;

        }
        $category_slug = $property_category;


    } else {
        // If the property is associated with only one category, we only have one choice
        if ( !isset( $property_categories[0] ) )
            $property_categories[0] = '';

        $property_category = $property_categories[0];

        if ( !is_object( $property_category ) )
            $property_category = new stdClass();

        if ( !isset( $property_category->slug ) )
            $property_category->slug = null;

        $category_slug = $property_category->slug;
    }

    $post_name = $post->post_name;

    if ( get_option( 'property_type_hierarchical_url', 0 ) ) {
        $selected_term = get_term_by( 'slug', $category_slug, 'property_type' );
        if ( is_object( $selected_term ) ) {
            $term_chain = array( $selected_term->slug );
            while ( $selected_term->parent ) {
                $selected_term = get_term( $selected_term->parent, 'property_type' );
                array_unshift( $term_chain, $selected_term->slug );
            }
            $category_slug = implode( '/', $term_chain );
        }
    }

    if( isset( $category_slug ) && empty( $category_slug ) )
        $category_slug = 'property_type';

    $category_slug = apply_filters( 'property_type_permalink_cat_slug', $category_slug, $post_id );

    $rewritereplace = array(
        $category_slug,
        $post_name
    );

    $permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
    $permalink = user_trailingslashit( $permalink, 'single' );

    $permalink = home_url( $permalink );
}
return apply_filters( 'property_permalink', $permalink, $post_id );
}
add_filter( 'post_type_link', 'icn_property_type_link', 1, 3 );

Related posts

Leave a Reply