• I have following pre_get_posts setup:

    
    add_action('pre_get_posts', function($query) {
      if($query->is_main_query() && !is_admin()) {
        $lang = get_query_var('lang', 'en');
        $meta_query[] = [
          'RELATION' => 'AND',
          [
            'key' => 'language',
            'value' => $lang,
            'compare' => '='
          ]
        ];
        $query->set('meta_query', $meta_query);
      }
    });

    And it works, but redirect_canonical redirects to the post even when the main query has 0 posts, instead of delivering a 404 page. The function redirects to the post with the same “slug” (postname), which is wrong. How can I correct this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Try using the “redirect_canonical” filter to manage where the redirect goes to. Your callback is passed the original and redirected URLs, and you can get the main query from global $wp_query. You can abort the redirect entirely by returning the original URL.

    Thread Starter Kristiyan Katsarov

    (@katsar0v)

    @bcworkz this might work yes, thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘redirect_canonical hook redirects, though unintended’ is closed to new replies.