Custom post admin filtering by post meta (the date)

I’ve created a custom post type for events that uses a custom field to retrieve the date for the event. It works ok and I’m able to filter the displayed results using meta_compare to compare the date value to the current date.

So far so good. I can also filter the admin end to display either future or past events (or both, by removing the meta compare) via a pre get posts filter.

What I want to do is to create an admin page that will display the past events. I’ve created the page (and menu item) with the following code:

add_action('admin_menu', 'add_concerts_page');
function add_concerts_page() {
    add_submenu_page('edit.php?post_type=concerts', 'Concerts Archive', 'Concerts Archive', 'manage_options', 'concerts-archive', 'my_archive_function' );
}

function my_archive_function() {}

But I don’t know how to write the function!! I can write a regular loop and filter the results, as I would do on the front-end, but all I need to do is to show the same information as the normal admin page, but showing events that have since passed.

This is how I filter the custom posts edit screen to show only future events:

function concerts_pre_get_posts($query) {
  if (is_admin()) {
    if (isset($query->query_vars['post_type'])) {
      if ($query->query_vars['post_type'] == 'concerts') {
          $query->set('meta_key', 'date_value');
          $query->set('orderby', 'meta_value');
          $query->set('order', 'ASC');
    $query->set('meta_compare', '>=');
          $query->set('meta_value', time());

      }
    }
  }
}
add_filter('pre_get_posts' , 'concerts_pre_get_posts');

I don’t really need to create a separate archive page for past events, just a link to toggle the query from past to future events on this page would be groovy, but I haven’t a clue how to go about doing this!

All help greatly appreciated!

1 Answer
1

You need to be storing your dates in some sortable format such as YYYY-MM-DD, or as a unix timestamp, otherwise you can’t do this just with a query. Once it’s in the format YYYY-MM-DD this should work. try this:

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

    if ( isset( $query->query_vars[ 'post_type' ] ) && $query->query_vars[ 'post_type' ] == 'concerts' ) {
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'order', 'ASC' );
        $query->set( 'meta_query', array(
            array(
                'key' => 'date_value',
                'value' => date( "Y-m-d" ),
                'compare' => '<=',
                'type' => 'DATE'
            )
        ) );
    }
}
add_filter( 'pre_get_posts', 'concerts_pre_get_posts' );

Leave a Comment