• Hi,
    I registered a new custom post type :

    // REGISTER CUSTOM POST TYPE
    register_post_type(‘classified’, array(
    ‘label’ => ‘Classifieds’,
    ‘singular_label’ => ‘Classified’,
    ‘public’ => true,
    ‘show_ui’ => true, // UI in admin panel

    ‘capability_type’ => ‘post’
    ));

    So when the permalinks are enabled;
    /wp/classified/the-post-slug/
    brings me to the single post.

    But I would like
    /wp/classified/

    to bring me on the classifieds index loop;

    exactly like when I use
    /wp/?post_type=classified.

    Actually it returns a 404 error.

    I did try to add new rewrite rules but I don’t get it.

    Could anyone help ?

Viewing 7 replies - 1 through 7 (of 7 total)
  • See that sivel helped you:

    add_filter('generate_rewrite_rules', 'customposttype_rewrites');
    function customposttype_rewrites($wp_rewrite) {
    	$newrules = array();
    	$newrules['mycustomposttype/?$'] = 'index.php?post_type=mycustomposttype';
    	$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
    }
    Thread Starter grosbouff

    (@grosbouff)

    Ok, it’s a first idea ! Thanks.
    My full problem is here.
    Please help !
    https://pastie.org/915594

    Hi – I’m trying to play around with Sivel’s code above but I’m struggling to achieve what I want. I have a custom post type named ‘work’, and a hierarchal taxonomy named ‘clients’ which is exclusive to the ‘work’ post panel. How could I achieve it so that visiting “domain.com/work/clients/client_x” would pull all the posts with that ‘client_x’ taxonomy? It’s the equivalent of visiting “domain.com/index.php?clients=client_x”, but I can’t get it to work. Here’s the code I’m trying:

    add_action('generate_rewrite_rules', 'work_list');
    function work_list($wp_rewrite) {
        $newrules = array();
        $newrules['work/?$'] = 'index.php?post_type=work';
        $newrules['work/page/?([0-9]{1,})/?$'] = 'index.php?post_type=work&paged=$matches[1]';
        $newrules['work/clients/?([0-9]{1,})/?$'] = 'index.php?clients=?$])';
        $wp_rewrite->rules = $newrules + $wp_rewrite->rules;
    }

    Thanks!

    Thread Starter grosbouff

    (@grosbouff)

    This is what I came up to.
    But not all is working.
    Could anyone help ?

    function yclads_rewrite_rules($wp_rewrite) {

    $newrules = array();

    //HOME
    $newrules[‘(classifieds)’] = ‘index.php?post_type=yclad’;
    //HOME + TIME
    $newrules[‘classifieds/(\d*)’] = ‘index.php?post_type=yclad&year=’ . $wp_rewrite->preg_index(1);
    $newrules[‘classifieds/(\d*)/(\d*)’] = ‘index.php?post_type=yclad&year=’ . $wp_rewrite->preg_index(1) . ‘&month=’ .$wp_rewrite->preg_index(2);
    $newrules[‘classifieds/(\d*)/(\d*)/(\d*)’] = ‘index.php?post_type=yclad&year=’ . $wp_rewrite->preg_index(1) . ‘&month=’ .$wp_rewrite->preg_index(2) . ‘&day=’ .$wp_rewrite->preg_index(3);

    ##ALL THOSE ABOVE DO NOT WORK

    //”PLACE AD”
    //TO FIX
    $newrules[‘classifieds/place-ad$’] = ‘index.php?post_type=yclad&place-ad’;

    //SINGLE ACTION
    $newrules[‘classifieds/action/(\w*)$’] = ‘index.php?post_type=yclad&yclad_action=’ . $wp_rewrite->preg_index(1);
    //SINGLE ACTION + SINGLE CAT
    //here the URL should be the breadcrumb to the child category and we should care only about the last category instance
    $newrules[‘classifieds/action/(\w*)/category/(\w*)$’] = ‘index.php?post_type=yclad&yclad_action=’ . $wp_rewrite->preg_index(1).’&yclad_category=’ . $wp_rewrite->preg_index(2);
    //SINGLE ACTIONS + SINGLE TAG
    $newrules[‘classifieds/action/(\w*)/tag/(\w*)$’] = ‘index.php?post_type=yclad&yclad_action=’ . $wp_rewrite->preg_index(1).’&=yclad_tag’ . $wp_rewrite->preg_index(2);

    //SINGLE CAT
    $newrules[‘classifieds/category/(\w*)$’] = ‘index.php?post_type=yclad&=yclad_category’ . $wp_rewrite->preg_index(1);

    ##++ what about combining this with author or time parameters ?
    ##++ have I to register those tags to allow custom permalinks ?how ?

    $wp_rewrite->rules = $newrules + $wp_rewrite->rules;

    }
    add_action(‘generate_rewrite_rules’, ‘yclads_rewrite_rules’);

    Thread Starter grosbouff

    (@grosbouff)

    help here. I’m searching for days !

    does
    [resolved] Writing $wp_rewrite->non_wp_rules to .htaccess? (12 posts)
    https://www.ads-software.com/support/topic/324792
    help?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘add rewrite rules for custom post types’ is closed to new replies.