add_action( 'init', 'add_property_and_location' );
function add_property_and_location() {
// register custom post type property
$labels = array(
'name' => _x('Properties', 'post type general name'),
'singular_name' => _x('Property', 'post type singular name'),
'add_new' => _x('Add New', 'book'),
'add_new_item' => __('Add New Property'),
'edit_item' => __('Edit Property'),
'new_item' => __('New Property'),
'all_items' => __('All Properties'),
'view_item' => __('View Property'),
'search_items' => __('Search Properties'),
'not_found' => __('No properties found'),
'not_found_in_trash' => __('No properties found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Properties'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'custom-fields', 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','page-attributes' ),
);
register_post_type('property',$args);
// register custom taxonomy location
$labels = array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search location' ),
'all_items' => __( 'All locations' ),
'parent_item' => __( 'Parent location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update location' ),
'add_new_item' => __( 'Add New location' ),
'new_item_name' => __( 'New location Name' ),
'menu_name' => __( 'location' ),
);
register_taxonomy('location',array('property'), array(
'public' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'hierarchical' => true,
'rewrite' => array('hierarchical' => true, 'slug' => 'properties' )
));
}
if(!class_exists('Refactord_add_rewrite_rules')):
class Refactord_add_rewrite_rules {
var $query_vars = array();
var $rules = array();
var $show_rules = false; //used for debugging rewrite rules
var $show_query_vars = false; //used for debugging query vars
function __construct($options = NULL){
if(!is_null($options)){
$this->init($options);
}
}
function init($options){
foreach($options as $key => $value){
$this->$key = $value;
}
if(!empty($this->rules)){
add_action('wp_head', array(&$this, 'flush_rules'));
add_action('generate_rewrite_rules', array(&$this, 'add_rules'));
}
if(!empty($this->query_vars)){
add_filter('query_vars', array(&$this, 'add_query_vars'));
}
if($this->show_rules){
add_action('wp_footer', array(&$this, 'show_rules'), 1);
}
if($this->show_query_vars){
add_action('wp_footer', array(&$this, 'show_query_vars'), 1);
}
}
function add_query_vars($query_vars){
foreach($this->query_vars as $var){
$query_vars[] = $var;
}
return $query_vars;
}
function add_rules(){
global $wp_rewrite;
$wp_rewrite->rules = $this->rules + $wp_rewrite->rules;
}
function rules_exist(){
global $wp_rewrite;
foreach($this->rules as $key => $rule){
if(!in_array($rule, $wp_rewrite->rules) || !key_exists($key, $wp_rewrite->rules)){
return FALSE;
}
}
return TRUE;
}
function flush_rules(){
global $wp_rewrite;
if(!$this->rules_exist()){
$wp_rewrite->flush_rules();
}
}
function show_rules(){
global $wp_rewrite;
echo "<pre>";
print_r($wp_rewrite->rules);
echo "</pre>";
}
function show_query_vars(){
global $wp_query;
echo "<pre>";
print_r($wp_query->query_vars);
echo "</pre>";
}
}
endif;
$options = array(
'rules' => array(
'properties/(.+?)/(for-sale)/page/?([0-9]{1,})/?$' => 'index.php?location=$matches[1]&type=$matches[2]&paged=$matches[3]',
'properties/(.+?)/(for-sale)/?$' => 'index.php?location=$matches[1]&type=$matches[2]',
'properties/(.+?)/(for-rent)/page/?([0-9]{1,})/?$' => 'index.php?location=$matches[1]&type=$matches[2]&paged=$matches[3]',
'properties/(.+?)/(for-rent)/?$' => 'index.php?location=$matches[1]&type=$matches[2]'
),
'query_vars' => array('type')
);
$add_rewrite_rules = new Refactord_add_rewrite_rules($options);