Site logo

Archived topic

Big problem with PHP filter "use static page as category archive"

3 replies · Started by Samuel on September 27, 2022

Viewing posts 1–4 of 4

Hello,

It's been 3 years I'm using this function to redirect category archive to static page with same URL :

add_filter('request', function( array $query_vars ) {
    if ( is_admin() ) {
        return $query_vars;
    }

    if ( isset( $query_vars['category_name'] ) ) {
        $pagename = $query_vars['category_name'];

        $query_vars = array( 'pagename' => "$pagename" );
    }

    return $query_vars;
} );

It seems to not working anymore and all my category pages are now redirected to my homepage !

The weird thing is I have a filter doing the same job with Tags and it's still working.

I've deactivated all plugin to see if there was any conflict but problem still there.

Please help !
Samuel

Sorry I think I've found the issue. I'm not using Yoast anymore and the plugin was remove the /category/ part in the archive page URLs.

So now I suppose the filter is not working because of this.

How could I modify the filter to work again ?

Problem is fixed : I managed to remove category prefix (as Yoast was doing) to make it work again.

I've added a rule to my htaccess :

# REMOVE CATEGORY PREFIX

RewriteRule ^category/(.+)$ http://mydomain.com/$1 [R=301,L]

# END REMOVE CATEGORY PREFIX

Don't know if it's the best solution ?

Open to your advices here.

But yeah, it's working now.

This archived topic is closed to new replies.