Greg
Greg

Reputation: 1055

Permalink from meta fields for custom post type wordpress

I have 5 custom fields in my wordpress instance that are boolean true/false values.

property_type_investment, property_type_office, property_type_rental, property_type_industrial, property_type_land

Is it possible to create a permalink for this custom post type that will check each of those meta value's are true - and if so append a value to the url hyphenated?

example: if property_type_investment, property_type_office, and property_type_land are true. I would like the permalink to be...

/listings/investment-office-land/post-title/

Here is my register post type.

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'),
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
        'taxonomies' => array( '' ),
        'has_archive' => true
    )
);

Upvotes: 1

Views: 1581

Answers (2)

csehasib
csehasib

Reputation: 365

You can test using by a simple way using by /%category%/%postname%/ in your admin panel->settings->permalink-> custom structure.

Upvotes: 0

Arka
Arka

Reputation: 591

you can rewrite the rule.

function prefix_investment_rewrite_rule() {
    add_rewrite_rule( 'listings/([^/]+)/investment-office-land', 'index.php?listings=$matches[1]&investment-office-land=yes', 'top' );
}

add_action( 'init', 'prefix_investment_rewrite_rule' );

//You should register the query var
function prefix_register_query_var( $vars ) {
    $vars[] = 'investment-office-land';
    return $vars;
}

add_filter( 'query_vars', 'prefix_register_query_var' );

// assign a template
function prefix_url_rewrite_templates() {
     if ( get_query_var( 'investment-office-land' ) && is_singular( 'listings' ) ) {
        add_filter( 'template_include', function() {
            return get_template_directory() . '/single-listings-investment-office-land.php';
        });
    }
}

add_action( 'template_redirect', 'prefix_url_rewrite_templates' );

Don't forget to flush the rewrite rule after register_post_type.

flush_rewrite_rules();  

Upvotes: 2

Related Questions