get_category_by_path example

$redirect['query'] = remove_query_arg( $_qv$redirect['query'] );
                                }
                            }
                        }
                    }
                }
            }
        } elseif ( is_single() && str_contains( $wp_rewrite->permalink_structure, '%category%' ) ) {
            $category_name = get_query_var( 'category_name' );

            if ( $category_name ) {
                $category = get_category_by_path( $category_name );

                if ( ! $category || is_wp_error( $category )
                    || ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
                ) {
                    $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
                    $redirect_obj = get_post( $wp_query->get_queried_object_id() );
                }
            }
        }

        // Post paging.
Home | Imprint | This part of the site doesn't use cookies.