Search code examples
wordpressloopsadvanced-custom-fieldscustom-wordpress-pagescustom-taxonomy

Wordpress filter by taxonomy and expired acf date field


I have a custom post type "events" with a custom taxonomy "events_cat" which has category-1, category-2 etc.

This is my code in taxonomy.php

<?php
    $today = date('Ymd');
    $cat_slug = get_queried_object()->slug;
    $args = array(
        'post_type'     => 'events',
        'nopaging'      => true,
        'meta_key'      => 'expiry',
        'tax_query'     => array(
            array(
                'taxonomy'  => 'events_cat',
                'field'     => 'slug',
                'terms'     => $cat_slug
            )
        ),
        'meta_query'    => array( 
            array(
                'key' => 'expiry', 
                'value' => $today, 
                'compare' => '>='
            )
        )
    );
    $events = new WP_Query( $args );
    if ( $events->have_posts() ) :
?>

<?php while ( $events->have_posts() ) : $events->the_post(); ?>

// my code

<?php endwhile; wp_reset_postdata(); ?>

<?php endif; ?>

What I need is to filter my post by category and if it is expired or not at the same time.

I explain better: I have one page with all expired posts and another one with all not expired posts. What I need is that if I click on a category in the "expired page" I just want the list of the expired posts and vice versa.

I was thinking of a condition to change the compare value depending on whether or not the post has expired but I can't figure out how I can do it.

EDIT

I tried such a thing but of course it doesn't work because $expiry is null.

if($expiry >= $today) {
    $compare = '>=';
} else {
    $compare = '<';
}

$args = array(
    'post_type'     => 'events',
    'nopaging'      => true,
    'meta_key'      => 'expiry',
    'tax_query'     => array(
        array(
            'taxonomy'  => 'events_cat',
            'field'     => 'slug',
            'terms'     => $cat_slug
        )
    ),
    'meta_query'    => array( 
        array(
            'key' => 'expiry', 
            'value' => $today, 
            'compare' => $compare
        )
    )
);

Does anyone have a better idea on how this can be done or point me in the right direction?


Solution

  • I finally solved the problem by using the $_GET super global variable. I don't know if it's the most correct method but it's the only one I've found. If anyone has a better idea please let me know.

    I added "?expired" to the category links of expired posts so I can do this:

    $today = date('Ymd');
    $compare = '>=';
    if( isset($_GET['expired'])) {
        $compare = '<';
    }
    args = array(
        'post_type'     => 'events',
        'nopaging'      => true,
        'meta_key'      => 'expiry',
        'tax_query'     => array(
            array(
                'taxonomy'  => 'events_cat',
                'field'     => 'slug',
                'terms'     => $cat_slug
            )
        ),
        'meta_query'    => array( 
            array(
                'key' => 'expiry', 
                'value' => $today, 
                'compare' => $compare
            )
        )
    );