I am working on a plug-in for my personal site and I would like it have a certain permalink structure (/projects/project-type/sub-type/project). To achieve this, I am using the request filter.

Inside my hook method, I am checking to make sure that the taxonom(y|ies) (project_type) exists before I load the post type (project). If it does not, the filter throws a 404 error and loads the theme's error template instead of just showing the project. Is it safe to do this?

public function request( $request ) {
    $dummy_query = new \WP_Query();
    $dummy_query->parse_query( $request );

    if ( ! $dummy_query->is_admin && isset( $request['project_type'] ) ) {
        $last_segment = basename( $request['project_type'] );

        if ( false === get_term_by( 'slug', $last_segment, 'project_type' ) ) {
            $types = explode( '/', substr( $request['project_type'], 0, strrpos( $request['project_type'], '/' ) ) );

            foreach( $types as $type ) {
                if ( false === get_term_by( 'slug', $type, 'project_type' ) ) {
                    header( 'HTTP/1.0 404 Not Found' );
                    locate_template( array( '404.php' ), true, true );

            $request['post_type'] = 'project';
            $request['project'] = $last_segment;
            $request['name'] = $last_segment;

    return $request;

This code was modified from this answer.

1 Answer 1


While I don't see particular problem with this, you do kind of break out of code flow at early point.

I would try to go for setting query to is_404 instead and let it reach template processing and 404 in template hierarchy naturally.

  • Thanks, Rarst. Something like this: $query = new WP_Query(); $query->is_404 = true;? Aug 19, 2012 at 18:35
  • @Joseph not quite, you need to set in main $wp_query object but I don't think request filter gives access to it, so different hook or global var.
    – Rarst
    Aug 19, 2012 at 19:23
  • Looking through the source, it seems that the best possible place would be in the template_redirect action. I could probably call add_action() from where the 404 error is being thrown right now and hook in a function that sets the query to 404. Aug 19, 2012 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.