0

This is both a "how do I?" and "how should I?" question, as I'm thinking of ways to achieve my goal but they seem rather messy, so I'm hoping there's a neater way.

Currently I have a custom post type, which has a custom taxonomy attached to it and uses a custom date field (like an "event date").

What I'd like to do is have a landing page for the post type, and somehow a combination of the following (ideally all grouped by month):

  • View posts from custom post type, taxonomy term A, with event date in the future
  • View posts from custom post type, taxonomy term A, with event date in the past
  • View posts from custom post type, taxonomy term B, with event date in the future
  • View posts from custom post type, taxonomy term B, with event date in the past

I'd prefer to avoid 4 pages, perhaps having a page that shows posts with event date in the current month, and navigation to go forward or backward a month.

I've got queries that compare the event date to the current date, distinguishing those in the future from those in the past, but I'm not sure a) how to display the current month with both forward and backward navigation, and b) how to group the posts by month (with the month name as a header).

I'm also unsure how to create the appropriate templates and link them together. I currently have an archive-{custom post type}.php template, but what other files would I need to create (if any), and assign URLs to them?

All advice appreciated. Thanks.

1 Answer 1

1

You can create the taxonomy-{$custom_taxonomy}.php that will handle all request for you custom taxonomy, and rely on query string to show events based on month.

The url can change based on your setting, but should be something like this:

http://yourdomain.com/custom-taxonomy/term-a/?month=2013-07

Hooking into pre_get_posts action (search codex) you can modify the query for that taxonomy and using WP_Query custom fields parameters you can show posts in the month required.

To show month as page heading you can use:

$m = isset( $_GET['month'] ) ? substr($_GET['month'], 5, 2) : date('n');
echo date_i18n('F', mktime(12, 0, 0, (int)$m, 1) );

with some logic you can create the link to show next and previous month:

$url = get_term_link( get_queried_object()->term_id, 'your-custom-tax');
$m = isset( $_GET['month'] ) ? substr($_GET['month'],5,2) : date('n');
$y = isset( $_GET['month'] ) ? substr($_GET['month'], 0,4) : date('Y');
$ts = mktime(12, 0, 0, (int)$m, (int)$y, 2);
$next_m_ts = $ts + (30*3600*24);
$prev_m_ts = $ts - (3*3600*24);
$next_url = add_query_arg( array('month' => date('Y-m',  $next_m_ts) ), $url );
$prev_url = add_query_arg( array('month' => date('Y-m',  $prev_m_ts) ), $url );

Hope this can help you, but, please, note that all the code here is not tested.

2
  • Thanks for this - I've been too busy to actually try it out yet, but will take a look as soon as I find time :)
    – Pete Gale
    Jul 25, 2013 at 21:48
  • Hacked about with it and turned it into something slightly different, as my needs changed the more I worked on it, but this was definitely the core of what I needed. Thanks!
    – Pete Gale
    Jul 27, 2013 at 1:21

Your Answer

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

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