0

I am building a site for a personal project and I have a series of Events created via a Custom Post Type.

I then need these events to be displayed by a custom meta value (Event Start Date) and in ASC order. I have managed to figure that out and the code I used is below:

/*CUSTOM ARCHIVE FOR EVENTS*/
    function events_pagesize( $query ) {
    // exit out if it's the admin or it isn't the main query
    if ( is_admin() || ! $query->is_main_query() )
    return;
    // so its not admin, and its the main query, is it the tr-events post archive?
    if ( is_post_type_archive( 'tr-events' ) ) {
    // it is!! Set the posts_per_page to 6
    $query->set( 'posts_per_page', 4 );
    $query->set( 'order' , 'asc' );
    $query->set( 'meta_key', 'wpcf-tr-order-date');
    $query->set( 'orderby', 'meta_value');
    return;
    }
    }
    add_action( 'pre_get_posts', 'events_pagesize', 1 );

My question is how to hide posts past a specific date AND how would I also create an Archive view to display Events that have completed? I've seen tons of posts about this and most options break pagination. This option as a function in the functions.php gets me 70% there.

1

I quess that it would be easiest for you to store date as unix timestamp in post meta - than you can easily filter posts by post meta less than current timestamp (or any other date timestamp).

See this example (not exactly for your code, but you'll catch the punch line:

$args = array(
   'post_type' => 'tr-events',
   'meta_key' => 'wpcf-tr-order-date',
   'orderby' => 'meta_value_num',
   'order' => 'ASC', //DESC, its up to you
   'meta_query' => array(
       array(
           'key' => 'wpcf-tr-order-date',
           'value' => strtotime('now'),
           'compare' => '<',
       )
   )
 );
 $query = new WP_Query($args);

As I mentioned, you'll have to rewrite this piece of code to $query->set(); format

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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