I have a super simple requirement here. I have added a meta field to all posts that will allow a url to be used to redirect the post (i have reasons for doing it this way). I am trying to trigger this on the action "the_post", but for some reason it doesnt seem to fire. Here is what ive added

function nb_checkredirect($post)
    if(get_post_meta( $post->ID, 'nb_postredirect', true ) != "") 
        header('Location: '.get_post_meta( $post->ID, 'nb_postredirect', true ));
add_action( 'the_post', 'nb_checkredirect' );

I have checked what gets output to the PHP error log and there doesnt appear to be anything going wrong.

  • are you sure that nb_checkredirect() is not being called? Could it be that get_post_meta( $post->ID, 'nb_postredirect', true ) isn't returning what you think it should? – Paul 'Sparrow Hawk' Biron Apr 2 '17 at 17:12
  • I modified the function so that die(); was the first line of the method and it still wasn't firing – Dan Hastings Apr 2 '17 at 17:12
  • When and how is the post_meta updated, and have you checked to be sure that it (or anything) is there when the_post action occurs? For example, if you're (successfully) adding the post_meta as part of the same process, it won't be found until the next time around. – CK MacLeod Apr 2 '17 at 17:27
  • in your template, are you calling one of the_post(), setup_postdata()? Because it is those functions that ultimately fire the_post, via WP_Query::setup_postdata(). – Paul 'Sparrow Hawk' Biron Apr 2 '17 at 17:30
  • 1
    Chances are good that the_post will fire after output has started, which means it's too late to set headers and redirect. template_redirect is the last reliable action that fires before template output starts. – Milo Apr 3 '17 at 1:13

Assuming that your post_meta is being properly added, that the post in question is a real post, and that the value of 'nb_postredirect' is a good URL, I'd try:

function nb_checkredirect( $post ) {

    //don't have to use get_post_meta!*
    if ( $post->nb_postredirect ) {

        wp_redirect( $post->nb_postredirect );




add_action( 'the_post', 'nb_checkredirect' );

Tested on my own site.

*Learned this just while checking up on this question - very nice.

  • the other answer was also correct, but it wasnt the hook that was the problem. This worked, but it seemed that using wp_redirect was what i needed. Not sure why the built in php method of it wasnt working – Dan Hastings Apr 5 '17 at 14:23

The redirect headers must be sent before the page is loaded. Please hook into template_redirect

function my_page_template_redirect()
    if( get_post_meta( $post->ID, 'nb_postredirect', true ) != "" )
        wp_redirect( get_post_meta( $post->ID, 'nb_postredirect', true ) );
add_action( 'template_redirect', 'my_page_template_redirect' );

Pls also see:

  1. https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
  2. https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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