1
2
3
<?php

I'm trying to enable custom rewriting for my custom post types, but it's quite hard !

GOALS :

1
2
3
4
5
6
7
8
9
10
11
12
13
/wp/classifieds/(...) should go to /index.php?post_type=classifieds(...);
eg. /wp/classifieds/ -> /index.php?post_type=classifieds
eg. /wp/classifieds/author/admin -> /index.php?post_type=classifieds&post_author=1

/wp/classifieds/ACTION_SLUG should go to /index.php?post_type=classifieds&classified_action=ACTION_SLUG;
eg. /wp/classifieds/offers -> /index.php?post_type=classifieds&classified_action=offers
eg. /wp/classifieds/offers/author/admin -> /index.php?post_type=classifieds&classified_action=offers

/wp/classifieds/.../category/CAT_1/CAT_1-SUBCAT_1/CAT1_SUBCAT_1_SUBSUBCAT_1 should go to /index.php?post_type=classifieds&...&classified_category=CAT1_SUBCAT_1_SUBSUBCAT_1;
eg. /wp/classifieds/category/cars/mercedes/break
eg. /wp/classifieds/offers/category/cars/mercedes/break
eg. /wp/classifieds/offers/category/cars/mercedes/break/author/admin

WHAT I CAME TO ACTUALLY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/wp/classifieds/ works
the others do not !

##

// Remember to flush_rules() when adding rules
function yclassifieds_rewrite_flush(){
  global $wp_rewrite;
     $wp_rewrite->flush_rules();
}
//TO FIX : hook on plugin activation, not init
add_action('init','yclassifieds_rewrite_flush');
//add_action('yclassifieds_activation','yclassifieds_rewrite_flush');


//rewrite action
function yclassifieds_rewrite_query_vars($public_query_vars) {
    $public_query_vars[] = 'classified_action';
  $public_query_vars[] = 'classified_category';
  $public_query_vars[] = 'classified_tag';
    return $public_query_vars;
}
add_filter('query_vars', 'yclassifieds_rewrite_query_vars');


function yclassifieds_rewrite_urls($wp_rewrite) {
  $newrules = array();
  $newrules['classifieds/?$'] = 'index.php?post_type=classified';
  $newrules['classifieds/(.+)'] = 'index.php?post_type=classified&classified_action=' . $wp_rewrite->preg_index(1);
  $newrules['classifieds/(.+)/category/(.+)'] = 'index.php?post_type=classified&classified_action=' . $wp_rewrite->preg_index(1).'&=classified_category' . $wp_rewrite->preg_index(2);
  $wp_rewrite->rules = $newrules + $wp_rewrite->rules;
}

add_action('generate_rewrite_rules', 'yclassifieds_rewrite_urls');

MY CUSTOM POST TYPES & TAXONOMIES REGISTRATION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
    // REGISTER CUSTOM POST TYPE
    register_post_type('classified', array(
      'label' => __('Classifieds','classifieds'),
      'singular_label' => __('Classified','classifieds'),
      'public' => true,
      'show_ui' => true, // UI in admin panel
      //'hierarchical'=>true, //?
      'capability_type' => 'post',
      //'rewrite' => array('slug' => 'classifieds','classifieds-slugs'), // Permalinks
      'supports' => array('title', 'editor', 'author','thumbnail','excerpts','trackbacks','custom-fields','comments')
    ));

    // REGISTER CUSTOM TAXONOMIES
    //actions
    register_taxonomy( 'classified_action', 'classified', array( 
      'hierarchical' => false,
      'label' => __('Actions','classifieds'),
      'singular_label' => __('Action','classifieds'), 
      'rewrite' => false
    ));
    //categories
    register_taxonomy( 'classified_category', 'classified', array( 
      'hierarchical' => true,
      'label' => __('Categories','classifieds'),
      'singular_label' => __('Category','classifieds'), 
      'rewrite' => false
    ));
    //tags
    register_taxonomy( 'classified_tag', 'classified', array( 
      'hierarchical' => false,
      'label' => __('Tags','classifieds'),
      'singular_label' => __('Tag','classifieds'), 
      'rewrite' => false
    ));
*/

?>