I'm using Wordpress and make a filter form in the category.php
page and get parameters with get_query_var
, my problem is in the Pagination when the user filters a parameter and wants to go to page 2, it redirects it to 404 instead of the category page (https://hexrom.com/roms/gameboy-advance/page/2/?title=pokemon). the problem is /page/2/?title=pokemon
doesn't load the category.php
Here is the code:
Code in function.php
:
add_action('init','wpse46108_register_param');
function wpse46108_register_param() {
global $wp;
$wp->add_query_var('title');
$wp->add_query_var('genre');
$wp->add_query_var('region');
$wp->add_query_var('console');
$wp->add_query_var('top');
}
And I have a custom query in category.php
page:
if (!empty(get_query_var('title'))) {
$ti = get_query_var('title');
}
if (!empty(get_query_var('genre'))) {
$ge = get_query_var('genre');
}
if (!empty(get_query_var('region'))) {
$reg = get_query_var('region');
}
if (!empty(get_query_var('console'))) {
$con = get_query_var('console');
}
if (!empty(get_query_var('top'))) {
$top = get_query_var('top');
}
if (!empty($ge) && empty($reg)) {
$args = array(
'post_type' => 'post',
'wpse18703_title' => '' . $ti . '',
'cat' => $catid,
'paged' => get_query_var('paged'),
'meta_query' => array(
array(
'key' => 'genre',
'value' => '' . $ge . '',
'compare' => '='
)
));
}
query_posts($args);
Update:
I also tried WP_Query
to load the post.
CodePudding user response:
In your code, you're using query_posts
to set the query arguments and run the query, but query_posts
is not recommended for use in WordPress because it modifies the main query and can lead to unexpected behavior.
Can you try using the WP_Query
class to create a new instance of the query and run it.
CodePudding user response:
The issue is with the way you are trying to construct the URL for the pagination. When the user filters the parameter and tries to go to page 2, the URL is being constructed as /page/2/?title=pokemon, which doesn't load the category.php page.
One way to fix this is by using the add_rewrite_rule() function in the function.php file to rewrite the URL in the desired format before it reaches the 404 page. For example, you can rewrite the URL to /roms/gameboy-advance/title/pokemon/page/2/
.
function custom_rewrite_rule() {
add_rewrite_rule('roms/([^/] )/title/([^/] )/page/([^/] )/?','index.php?pagename=roms&category=$matches[1]&title=$matches[2]&paged=$matches[3]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
You should also add the custom query variables to the rewrite rule so that they can be passed to the category.php page.
function custom_query_vars($vars) {
$vars[] = "title";
$vars[] = "genre";
$vars[] = "region";
$vars[] = "console";
$vars[] = "top";
return $vars;
}
add_filter('query_vars', 'custom_query_vars');
Also, in your category.php you should update the get_query_var call to use these custom query variable for pagination and filtering,
$title = get_query_var('title');
$genre = get_query_var('genre');
$region = get_query_var('region');
$console = get_query_var('console');
$top = get_query_var('top');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
And don't forget to flush the rewrite rules by visiting the "Settings > Permalinks" page in the WordPress admin after you add the rewrite rules.