Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm a bit confused. I have a custom-post-type named wr_event. I'm using the code underneath to loop through all "upcoming" events (defined by the meta_key `event_date).

The problem I have now is that I want not only to show all upcoming posts, but all posts of the current year.

if ( have_posts() ): the_post();

    $current_year = mktime(0, 0, 0, 1, 1, date('Y'));
    //$last_year = mktime(0, 0, 0, 1, 1, date('Y') - 1);

    // Show all upcoming events of the current year
    /*$args = array(
        'post_type' => 'wr_event',
        'posts_per_page' => -1,
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'DSC',
        'meta_value' => $current_year,
        'meta_compare' => '>',
        'taxonomy' => 'event_type',
        'term' => get_queried_object()->slug
    );*/

    // Show all upcoming and taken place events of the current year
    $args = array(
        'post_type' => 'wr_event',
        'posts_per_page' => -1,
        /*'meta_key' => 'event_date',
        'orderby' => 'meta_value_num',
        'order' => 'DSC',
        'meta_value' => $last_year,
        'meta_compare' => '>',*/
        'taxonomy' => 'event_type',
        'term' => get_queried_object()->slug
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'inc/event', 'item' );
    endwhile;
    wp_reset_postdata();

    else: ?>

    <p class="wrapper no-items">
            There are no posts found
        </p>

Any idea how to do so? I simply want to list all events filterd by the current taxonomy-term of the current_year. And I can't seem to find out how to do so?

UPDATE

Thank you … but how would this look?

 $args = array(
                'post_type' => 'wr_event',
                'posts_per_page' => -1,
                'meta_query' => array( array(
                    'key' => 'event_date',
                    'value' => $current_year,
                    'compare' => '>=',
                    'type' => 'numeric'
                ) ),
                'taxonomy' => 'event_type',
                'term' => get_queried_object()->slug
            );
share|improve this question
    
How is event_date stored? What does the value look like? –  s_ha_dum Dec 8 '12 at 14:58

1 Answer 1

up vote 0 down vote accepted

Looking at what you've tried (in your commented code), I think you're close. I'm going to go out on a limb and guess that your event_date meta field is not stored as a numeric timestamp, which is what you were comparing against. Assuming it is stored as a text date like '2012-12-08' (and if not, it probably should be), here's what you should add to your $args array:

'meta_query' => array( array(
    'key' => 'event_date',
    'value' => date( 'Y-m-d', $current_year ),
    'compare' => '>=',
    'type' => 'date'
) )

Edit:

Knowing now that your data is stored as a numeric unix timestamp, try this:

'meta_query' => array( array(
    'key' => 'event_date',
    'value' => $current_year,
    'compare' => '>=',
    'type' => 'numeric'
) )

*Note: old answer left in for future readers who may be storing their date as a string.

share|improve this answer
    
Well, thank you. My event_date is stored as timestamp. However either way I don't get ANY results from this loop. There are no posts returned. I'm always inside the "else" statement. As if there were no posts. There are no posts found <- see my little update above. –  mathiregister Dec 8 '12 at 16:44
    
@mathiregister thanks for clarifying. I adjusted my answer to give you something else to try. If that doesn't work, I would install the Debug Bar plugin and look at the SQL that the page is generating. Then try running that SQL query in MySQL directly. If you're getting results there, it's a template issue. If you're not getting results there, post the SQL query and I or someone else can see where things are going wrong. –  Matthew Boynes Dec 8 '12 at 17:30
    
Thank you, however still no luck. Again no posts are returned. See my update above. Is this the right way I'm using this meta_query? I'll try the Debug Bar plugin tomorrow. Thank you in the meantime. Please make sure to check back here :) –  mathiregister Dec 8 '12 at 18:03
    
I would also try removing the 'taxonomy' and 'term' parameters for now. Limit your variables to find the source. –  Matthew Boynes Dec 8 '12 at 18:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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