url rewriting - Change custom post type slugs, with categorytaxonomy before post type name

I'm currently working on a website which is a collective name of 2 schools. That's why I've renamed the t

I'm currently working on a website which is a collective name of 2 schools. That's why I've renamed the taxonomy categories to schools. So in every post of a custom post type I can select the right school/category.

Now I have 3 custom post types in this theme.

  1. Agenda
  2. Schoolgids (schoolguide)
  3. Vacatures (vacancies)

Everything looks like it's working good, but only the URL-structure is not the way I want it to be. The way I want it, is for example: domainname/schoolname/post-type/post-name. My custom post type(s) is/are looking like this:

Post type (agenda for example):

function custom_post_agenda() {
    register_post_type( 'agenda',
        array('labels' => array(
            'name' => __('Agenda', 'raadhuiswp'),
            'singular_name' => __('Agenda', 'raadhuiswp'),
            'all_items' => __('Alle agendapunten', 'raadhuiswp'),
            'add_new' => __('Nieuw agendapunt', 'raadhuiswp'),
            'add_new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
            'edit' => __( 'Wijzig', 'raadhuiswp' ),
            'edit_item' => __('Wijzig agendapunt', 'raadhuiswp'),
            'new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
            'view_item' => __('Toon agendapunt', 'raadhuiswp'),
            'search_items' => __('Zoeken naar agendapunten', 'raadhuiswp'),
            'not_found' =>  __('Niks gevonden in de database.', 'raadhuiswp'),
            'not_found_in_trash' => __('Niks gevonden en de prullenbak.', 'raadhuiswp'),
            'parent_item_colon' => ''
            ),
            'description' => __( 'Een post type voor agendapunten', 'raadhuiswp' ),
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 8,
            'menu_icon' => 'dashicons-calendar-alt',
            'rewrite'   => array( 'slug' => '%school%/agenda',
            'with_front' => true ),
            'has_archive' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'show_in_rest' => true,

            'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'revisions', 'page-attributes')
        )
    );

    register_taxonomy_for_object_type('category', 'agenda');
    register_taxonomy_for_object_type('post_tag', 'agenda');
}

add_action( 'init', 'custom_post_agenda');

Now I've already managed to rewrite the slug for the post types with the following function:

Function:

function school_post_type_link($link, $post) {

      if ($post->post_type == 'agenda') {

            if ($school = get_the_terms($post->ID, 'category')) {


                $link = str_replace('%school%', array_pop($school)->slug, $link);

                return $link;
            }

    } elseif ($post->post_type == 'vacatures') {

        if ($school = get_the_terms($post->ID, 'category')) {

            $link = str_replace('%school%', array_pop($school)->slug, $link);

            return $link;
        }

        } elseif ($post->post_type == 'schoolgids') {

            if ($school = get_the_terms($post->ID, 'category')) {

                $link = str_replace('%school%', array_pop($school)->slug, $link);

                return $link;
            }

    } else {
        return $link;
    }
}

add_filter('post_type_link', 'school_post_type_link', 1, 3);

This way, I replace the '%school%' for the for the category name. And it seem to work:

So what I'm basicly doing is, checking if the post type is one of my custom post types, and if so, I replace the slug of it.

Now when I click view page, I keep getting the page not found (404) page. I have no idea why this is, because I can edit the post in WordPress itself without any problems.

Any idea what could cause this? Thanks in advance!

I'm currently working on a website which is a collective name of 2 schools. That's why I've renamed the taxonomy categories to schools. So in every post of a custom post type I can select the right school/category.

Now I have 3 custom post types in this theme.

  1. Agenda
  2. Schoolgids (schoolguide)
  3. Vacatures (vacancies)

Everything looks like it's working good, but only the URL-structure is not the way I want it to be. The way I want it, is for example: domainname/schoolname/post-type/post-name. My custom post type(s) is/are looking like this:

Post type (agenda for example):

function custom_post_agenda() {
    register_post_type( 'agenda',
        array('labels' => array(
            'name' => __('Agenda', 'raadhuiswp'),
            'singular_name' => __('Agenda', 'raadhuiswp'),
            'all_items' => __('Alle agendapunten', 'raadhuiswp'),
            'add_new' => __('Nieuw agendapunt', 'raadhuiswp'),
            'add_new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
            'edit' => __( 'Wijzig', 'raadhuiswp' ),
            'edit_item' => __('Wijzig agendapunt', 'raadhuiswp'),
            'new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
            'view_item' => __('Toon agendapunt', 'raadhuiswp'),
            'search_items' => __('Zoeken naar agendapunten', 'raadhuiswp'),
            'not_found' =>  __('Niks gevonden in de database.', 'raadhuiswp'),
            'not_found_in_trash' => __('Niks gevonden en de prullenbak.', 'raadhuiswp'),
            'parent_item_colon' => ''
            ),
            'description' => __( 'Een post type voor agendapunten', 'raadhuiswp' ),
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 8,
            'menu_icon' => 'dashicons-calendar-alt',
            'rewrite'   => array( 'slug' => '%school%/agenda',
            'with_front' => true ),
            'has_archive' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'show_in_rest' => true,

            'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'revisions', 'page-attributes')
        )
    );

    register_taxonomy_for_object_type('category', 'agenda');
    register_taxonomy_for_object_type('post_tag', 'agenda');
}

add_action( 'init', 'custom_post_agenda');

Now I've already managed to rewrite the slug for the post types with the following function:

Function:

function school_post_type_link($link, $post) {

      if ($post->post_type == 'agenda') {

            if ($school = get_the_terms($post->ID, 'category')) {


                $link = str_replace('%school%', array_pop($school)->slug, $link);

                return $link;
            }

    } elseif ($post->post_type == 'vacatures') {

        if ($school = get_the_terms($post->ID, 'category')) {

            $link = str_replace('%school%', array_pop($school)->slug, $link);

            return $link;
        }

        } elseif ($post->post_type == 'schoolgids') {

            if ($school = get_the_terms($post->ID, 'category')) {

                $link = str_replace('%school%', array_pop($school)->slug, $link);

                return $link;
            }

    } else {
        return $link;
    }
}

add_filter('post_type_link', 'school_post_type_link', 1, 3);

This way, I replace the '%school%' for the for the category name. And it seem to work:

So what I'm basicly doing is, checking if the post type is one of my custom post types, and if so, I replace the slug of it.

Now when I click view page, I keep getting the page not found (404) page. I have no idea why this is, because I can edit the post in WordPress itself without any problems.

Any idea what could cause this? Thanks in advance!

Share Improve this question asked Jul 26, 2019 at 8:07 Loosie94Loosie94 2312 silver badges11 bronze badges 3
  • Have you flushed the rewrite rules? Just visit the Permalink Settings page. – Sally CJ Commented Jul 26, 2019 at 10:15
  • @SallyCJ Yes, everytime I change something – Loosie94 Commented Jul 26, 2019 at 12:48
  • Sorry, I didn't really notice the rewrite structure. But see my answer and let me know. – Sally CJ Commented Jul 26, 2019 at 14:42
Add a comment  | 

1 Answer 1

Reset to default 1

The error 404 happens because you haven't registered the %school% rewrite tag — you need to register it so that WordPress will know what to replace it with when generating the rewrite rules — i.e. it doesn't remain as %school% in the generated rewrite rules. And you can register the tag using add_rewrite_tag():

// Add this to custom_post_agenda(), before you call register_post_type().
add_rewrite_tag( '%school%', '([^/]+)' );

Secondly, when you register the post type, the has_archive should be set to a unique slug such as agendas and not just true because if you use 'has_archive' => true, the archive slug would be %school%:

register_post_type( 'agenda', array(
    'has_archive' => 'agendas',
    // ...
) );

And third, because the post type's rewrite slug starts with the ([^/]+) (i.e. a category slug), you need the following filter to remove unnecessary rewrite rules (for that post type) which will conflict with Page's (and possibly other post types' and/or taxonomies') rewrite rules:

// Add this after custom_post_agenda() or somewhere else where appropriate.
add_filter( 'agenda_rewrite_rules', function( $rules ){
    // Remove all rules which don't have "/agenda/".
    unset( $rules['([^/]+)/page/?([0-9]{1,})/?$'] );
    unset( $rules['([^/]+)/comment-page-([0-9]{1,})/?$'] );
    unset( $rules['([^/]+)/?$'] );

    return $rules;
} );

UPDATE

I forgot to mention that you need to apply the above filter (i.e. <post type>_rewrite_rules) to other post types with the same rewrite slug format (%school%/<post type> like %school%/agenda). So remove the above code from your functions file and use this instead:

function fix_school_rewrite_rules( $rules ) {
    // Remove all rules which don't have "/<post type>/".
    unset( $rules['([^/]+)/page/?([0-9]{1,})/?$'] );
    unset( $rules['([^/]+)/comment-page-([0-9]{1,})/?$'] );
    unset( $rules['([^/]+)/?$'] );

    return $rules;
}
add_filter( 'agenda_rewrite_rules', 'fix_school_rewrite_rules' );     // for "agenda" CPT
add_filter( 'vacatures_rewrite_rules', 'fix_school_rewrite_rules' );  // for "vacatures" CPT
add_filter( 'schoolgids_rewrite_rules', 'fix_school_rewrite_rules' ); // for "schoolgids" CPT

And don't forget to flush the rewrite rules. And once again, for each CPT, the has_archive should be set to a unique slug.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745278147a4620151.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信