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.

1 Answer
1

Based on the comments of @Milo and using the rewrite analyzer plugin I found what I understand as a conflict in the rewrite rules?. The following url:

exams/high-school/city-name/subject/math/

matches the following rules

Pattern:
    (.?.+?)/page/?([0-9]{1,})/?$
Subtitution:
    pagename: exams/high-school/city-name/subject/math
    paged: 1

Pattern:
    (.?.+?)(/[0-9]+)?/?$ 
Subtitution:
    pagename: exams/high-school/city-name/subject/math/page
    page: /1

Pattern: 
    exams/([^/]+)/([^/]+)/subject/([^/]+)/?$    
Subtitution:
    pagename: examns
    level: high-school
    city: city-name
    subject: math
    page: 1

The last one is the one I’m trying to create. I’m not an expert in the rewriting rules but I think the second one was activating first and producing the wrong redirection to http://www.example.com/exams/1 or http://www.example.com/exams/2.

So, I changed the second of my rules to:

exams/([^/]+)/([^/]+)/subject/([^/]+)/page/?([0-9]{1,})/?$'  => 'index.php?pagename=exams&level=$matches[1]&city=$matches[2]&subject=$matches[3]&pages=$matches[4]

Note the var as ‘pages‘ in plural. It’s working properly. However, the rewrite analyzer plugin is not detecting my rules. ‘?’

In fact, if I change ‘page’ by ‘pages’ or ‘whateverIwant‘ and register that varname and getting it in my plugin:

$args = array(  'post_type'         => 'attachment',
    'posts_per_page'    => 2,
    'paged'             => ( get_query_var('whateverIwant') ? get_query_var('whateverIwant') : 1 ),
    'orderby'           => 'post_title',
    'order'             => 'ASC',
    'post_status'       =>'published'
);

Also work!. So, the first set of rules I created was in conflict with something inside wordpress. I have much to learn.

PS: I will accept any question with a solid argument solving this problem.

Leave a Reply

Your email address will not be published. Required fields are marked *