page is not redirecting

You need to redirect before any content is sent to the browser. If you had debugging enabled, you’d see errors about headers already being sent.

The question is light on detail but if there is a way to avoid the secondary loop and use the main query this should work (though I question the logic):

add_action(
  'template_redirect',
  function() {
    if (is_single()) {
      wp_safe_redirect(home_url());
      exit;
    }
  }
);

If you must have the secondary Loop, you need to run the query and make the decision about the redirect before get_header in your theme files.

Leave a Comment