Archived topic
Big problem with PHP filter "use static page as category archive"
3 replies · Started by Samuel on September 27, 2022
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.
Hi Samuel,
Glad you resolved the issue.
Yes, that's still the current way to use a static page as a category archive: https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/
If it's working, then that should be good.