Skip to main content
added 976 characters in body
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

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.

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;
} );

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.

Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

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;
} );