I’m trying to list attachments based on different taxonomies. Right now it only work for the basic url (no pagination added). For example
http://www.example.com/exams/high-school/city-name/subject/math/
First, I added my custom rewrite rule in functions.php as follows:
function add_rewrite_rules($aRules) {
$new_rules = array(
// rule 1
'exams/([^/]+)/([^/]+)/subject/([^/]+)/?$' =>
'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]',
// rule 2
'exams/([^/]+)/([^/]+)/subject/([^/]+)/page/([0-9]+)/?$' =>
'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]&page=$matches[4]'
);
return $new_rules + $aRules;
}
// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');
I go to wordpress, permanent links and click on save each time I change that function.
Second, I added the query vars I’m using in the URL (also in functions.php):
// represents for example the name of the product category
function add_query_vars($aVars) {
$aVars[] = "level";
$aVars[] = "city";
$aVars[] = "subject";
$aVars[] = "page";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
Third, I created a page named ‘exams’ and a plugin to get the attachmets that only work if the page you are watching is exams:
// Hooks a function to a filter action, 'the_content' being the action...
add_filter('the_content','me_get_listado');
// Callback function
function me_get_listado($content)
{
// Checking if on post page.
if ( is_page('exams') )
{
// get the query vars
if ( get_query_var( 'level' ) && get_query_var( 'city' ) && get_query_var( 'subject' ) )
{
// get_post ($args) ...
}
return $content; // the new content
} else {
// else on blog page / home page etc, just return content as usual.
return $content;
}
}
Well, this is working almost as expected. When I go to
http://www.example.com/exams/high-school/city-name/subject/math/
I get the attachmet of ‘math’ from ‘city-name’ for the ‘high-school’. But when I go to:
http://www.example.com/exams/high-school/city-name/subject/math/page/1
or:
http://www.example.com/exams/high-school/city-name/subject/math/page/2
I’m redirected to:
http://www.example.com/exams/1 or http://www.example.com/exams/2
So, nothing work.
I wondering what is wrong with the rewrite rules I have addeed? Or if I’m interfering with the WordPress logic?
NOTE: ‘level’, ‘city’ and ‘subject’ are custom taxonomies.