I'm trying to list attachments based on different taxonomies. Right now it only work for the basic url (no pagination added). For example


First, I added my custom rewrite rule in functions.php as follows:

function add_rewrite_rules($aRules) {
    $new_rules = array(
    // rule 1
    'exams/([^/]+)/([^/]+)/subject/([^/]+)/?$'  => 
    // rule 2
    'exams/([^/]+)/([^/]+)/subject/([^/]+)/page/([0-9]+)/?$'  => 

    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...

// 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


I get the attachmet of 'math' from 'city-name' for the 'high-school'. But when I go to:




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.

  • what happens if you add the trailing slash? page is already a WordPress query var, you can try making the query var and url segment unique to see if it works. also note that if you've registered taxonomies level, city, and subject, those query vars already exist and don't need to be added.
    – Milo
    Commented May 1, 2015 at 17:18
  • Same behavior adding / at the end. Regarding the taxonomies, I created them using the Enhanced Media Library plugin. If I do not register them I can not retrieve the vars in my plugin, although I think the problem is not here but in the rewrite rules or the scheme I'm using
    – pQB
    Commented May 1, 2015 at 17:29
  • 1
    I meant that part of the process of registering a taxonomy already adds query vars, though the plugin is probably adding them under different names. as for the rules- check them with the rewrite analyzer plugin, and maybe hook parse_request and parse_query and dump the contents of the arguments to get a better idea of what is happening behind the scenes.
    – Milo
    Commented May 1, 2015 at 17:52
  • I installed the plugin and it seems there is another rule that matches the same URL: (.?.+?)(/[0-9]+)?/?$. I will dig a bit more.
    – pQB
    Commented May 2, 2015 at 10:33

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:


matches the following rules

    pagename: exams/high-school/city-name/subject/math
    paged: 1

    pagename: exams/high-school/city-name/subject/math/page
    page: /1

    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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.