WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
Thanks, Rarst. Something like this: $query = new WP_Query(); $query->is_404 = true;? – Joseph Leedy Aug 19 '12 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 '12 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. – Joseph Leedy Aug 19 '12 at 21:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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