Monthly Archives: July 2016

Next & Previous Post Links in the Same Taxonomy

If you use Custom Post Types with custom taxonomies in WP, you’ve no doubt wanted to add Next & Previous buttons on the single-cake.php template that keep the same taxonomy term as the page they sit on.

As a loose example, let’s use cake. cake is our CPT, and we have a custom taxonomy of flavor. We have a few terms there of chocolate, lemon, and strawberry.

We’re looking at a chocolate cake, and we want a button at the bottom of the single-cake.php template that says Next Chocolate Cake1. So we need a way to link to the next cake that’s also a chocolate cake.

$prev_post = get_previous_post(true, false, 'flavor');
$next_post = get_next_post(true, false, 'flavor');
  • true means we want the next/prev post to be of the same term (‘flavor’ in our case)
  • false means we don’t want to exclude any ID’s here
  • flavor is our taxonomy

In the past, I’d have some something with a WP_Query loop, but now I know this exusts, I don’t need to!

There’s more info on get_previous_post() and get_next_post(), so read the docs!


  1. You’ll need to do something with get_the_terms() to get the current taxonomy term. 

Event Date Sorting Columns for WordPress CPTs

On a recent client project, one of the requirements was adding a couple admin columns for a custom pst type type to sort things by start date and end date.

It was a slight faff to work out how this should be done, so I’m plonking it here for me to find in the future.

Columns

The complication here was wanting to show the date in a human-readable format whilst letting the sorting happen using the unix time stamp.

So, a few things to note:

  • event is the custom post type name. If it were cake, every action and filter name would need to be cake instead of event.
  • first_show_date and last_show_date are custom fields storing the Unix time stamp

Add a column to the admin

function event_cpt_add_columns($defaults) {
  $defaults = array(
    'cb'             => '<input type="checkbox" />',
    'title'          => 'Title',
    'firstshowdate'  => 'First Show Date',
    'lastshowdate'   => 'Last Show Date',
    'date'           => 'Date',
  );

  return $defaults;
}

Add something to that new column

function event_cpt_column_data($column_name, $post_ID) {
  if ($column_name == 'firstshowdate') {
    echo date('jS F Y', get_post_meta('first_show_date', $post_ID), true);
  }
  if ($column_name == 'lastshowdate') {
    echo date('jS F Y', get_post_meta('last_show_date', $post_ID), true);
  }
}
add_filter('manage_event_posts_columns', 'event_cpt_add_columns', 10);
add_action('manage_event_posts_custom_column', 'event_cpt_column_data', 10, 2);

function event_cpt_register_sortable($columns) {
  $columns['firstshowdate'] = 'firstshowdate';
  $columns['lastshowdate'] = 'lastshowdate';
  return $columns;
}
add_filter("manage_edit-event_sortable_columns", "event_cpt_register_sortable" );

Enable sorting…

… using the original value of the custom field, not it’s formatted counterpart.

function event_cpt_orderby( $query ) {
  if (!is_admin())
  return;

  $orderby = $query->get('orderby');

  if ('firstshowdate' == $orderby ) {
    $query->set('meta_key','first_show_date');
    $query->set('orderby','meta_value_num');
  }

  if ('lastshowdate' == $orderby ) {
    $query->set('meta_key','first_show_date');
    $query->set('orderby','meta_value_num');
  }
}
add_action('pre_get_posts', 'event_cpt_orderby');

If this can be improved, do let me know!