Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to redirect to a specific post based on a GET parameter. The value of the GET parameter is the value of a specific custom field.

For example:

http://somedomain.com/wordpress/?fid=xyzzy

I have this working when a post matches. But, it's not working properly when there is no match.

    add_filter('query_vars', 'fid_query_vars');
function fid_query_vars($vars) {
    // add fid to the valid list of variables
    $new_vars = array('fid');
    $vars = $new_vars + $vars;
    return $vars;
}

and

add_action('parse_request', 'fid_parse_request');
function fid_parse_request($wp) {
    // only process requests with "fid"
    if (array_key_exists('fid', $wp->query_vars) && $wp->query_vars['fid'] != '') {
        $args = array('post_type' => 'faculty_profile', 'meta_key' => 'wid', 'meta_value' => $wp->query_vars['fid'] , 'numberposts' => 1);
        $redirect_to_post = get_posts($args);
if (!empty($redirect_to_post ) ) {
        foreach ($redirect_to_post as $p) {
            $link = get_permalink($p->ID);
                        wp_redirect( $link , 301 );
            exit;
        }

        }
else {
    $url = 'http://cnn.com/';
    wp_redirect( $url , 404 );
        }

    }

I'd expect this to redirect to cnn.com with a 404 error, if there is no match. But, instead, I get the blog's homepage, with a status of 200.

Any ideas? thanks.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

remove the else after the foreach:

add_action('parse_request', 'fid_parse_request');
function fid_parse_request($wp) {
    // only process requests with "fid"
    if (array_key_exists('fid', $wp->query_vars) && $wp->query_vars['fid'] != '') {
        $args = array('post_type' => 'faculty_profile', 'meta_key' => 'wid', 'meta_value' => $wp->query_vars['fid'] , 'numberposts' => 1);
        $redirect_to_post = get_posts($args);
        if (!empty($redirect_to_post ) ) {
            foreach ($redirect_to_post as $p) {
                $link = get_permalink($p->ID);
                wp_redirect( $link , 301 );
                exit;
            }
        }
        $url = 'http://cnn.com/';
        wp_redirect( $url , 404 );
        exit;
    }else{
        return $wp;
    }
}
share|improve this answer
    
Thanks. This makes sense, but it doesn't seem to work. I get a blank page (with a 404 header, which is good). I don't really want to redirect to cnn.com -- I just want to serve the WordPress 404 error page. I suppose I don't really want to use wp_redirect, I just need to serve up a 404 somehow. But I am not sure what function to use for this. –  wjdennen Apr 16 '11 at 12:11
    
adding $wp->is_404 = true; does not seem to help? –  wjdennen Apr 16 '11 at 12:31
    
I can make this work by just accessing the url like this: somedomain.com/wordpress/something-bogus/?fid=xyzzy –  wjdennen Apr 16 '11 at 12:50
add comment

Your Answer

 
discard

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.