I am creating gallery pages for events and csr-events custom post type.

Now it has three types of pages:

  1. www.example.com/gallery
  2. www.example.com/gallery/events
  3. www.example.com/gallery/csr-events

I have programatically created pages for the above slugs using wp_insert_post: Example for events gallery:

            'comment_status' => 'closed',
            'ping_status' => 'closed',
            'post_author' => 1,
            'post_parent' => $page['parent'],
            'post_name' => $page['slug'],
            'post_title' => $page['title'],
            'post_status' => 'publish',
            'post_type' => $page['post-type'],
            'page_template' => $page['template'],

So the pagenames are gallery,events and csr-events.

I have set gallery as a parent page and events and csr-events as child of gallery.

And then in template-gallery.php I check the $wp_query->query_vars['pagename'] Which gives me gallery or events or csr-events. And based on this I write a query to get images.

I have created an archive list for the these 3 gallery pages by making some changes in this plugin which returns URL in this format :

  1. http://www.example.com/gallery/2015/09
  2. http://www.example.com/gallery/events/2015/08
  3. http://www.example.com/gallery/csr-events/2015/09
  4. For other archive pages having custom post type it returns : http://www.example.com/events/2015/09

and I made changes in the rewrite rule of the plugin which works for other custom post types but does not work for gallery pages :

$line_separated = implode( '|', $post_types );//works for events/
$newrules['(' . $line_separated . ')/(\d*)/(\d*)$'] = 'index.php?post_type=$matches[1]&m=$matches[2]$matches[3]';

What rewrite rule should I use here for http://www.example.com/gallery/events/2015/08 to work.

I tried this

$newrules['gallery/events/(\d*)/(\d*)$'] = 'index.php?pagename=events&m=$matches[1]$matches[2]';

but it just loads gallery/events and removes the year and month What rewrite rule can I use to load the events page and persist the url such that I can get year and month in the m .

Note : gallery is not a custom post type here. It is just a page.

1 Answer 1


To determine the best filter to use, you have to determine what those pages are. Is is_page() true? Use the page_template filter. Is is_post_type_archive() true? Use the archive_template filter.

All of the query type-specific template filters are listed here.

You can also use the generic template_include, which fires for all types.

Within any of these filters you can check the same conditions you need to determine if you need to load the template-gallery.php template.

  • Thanks @Milo you have been very helpful. What can be the condition to make sure it loads this template for for these urls. Because is_page will not work for this case.
    – roshan
    Nov 12, 2015 at 20:04
  • These url's were created by me by just changing the output of the plugin. So these url's got no data registered with wordpress.
    – roshan
    Nov 12, 2015 at 20:21
  • That plugin generates some type of main query. var_dump($wp_query) for those requests, what do you see?
    – Milo
    Nov 12, 2015 at 20:30
  • I did a var_dump($wp_query) in template_include and here is the output pastebin.com/i39xxsPv .
    – roshan
    Nov 12, 2015 at 20:41
  • is_404 is true for those queries. that plugin seems to be doing something incorrectly.
    – Milo
    Nov 12, 2015 at 20:45

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.