• Resolved TrishaM

    (@trisham)


    Searched all the posts here related to using custom event dates in queries and didn’t find an answer to my specific problem, hoping someone can help.

    I have a custom post type for events, with custom fields to hold the event start date and end date.

    I have query that finds all matching posts and lists them in order of their start date as long as the start date is a future date (old events don’t show), however I have two problems:

    The events drop off on the date that they start even though I’m using <= in my query, not just < (they should display through that date and drop off on the following date); and

    For multi-day events, they should remain in the list until the event has ended (the day after the date the event ends) but they still drop off the list on the date that they start;

    Here’s my code:

    global $post;
    
    $eventdate = get_post_meta($post->ID,'event_date_start',true);
    $eventdate2 = get_post_meta($post->ID,'event_date_end',true);
    $eventlocation = get_post_meta($post->ID,'event_location',true);
    $eventaddress = get_post_meta($post->ID,'event_location_address',true);
    $eventdirections = get_post_meta($post->ID,'event_location_directions',true);
    $eventcontact = get_post_meta($post->ID,'event_contact',true);
    $eventcontactph = get_post_meta($post->ID,'event_contact_phone',true);
    $eventcontactem = get_post_meta($post->ID,'event_contact_email',true);
    $eventnumdays = get_post_meta($post->ID,'event_number_of_days',true);
    
    $myposts =           get_posts('post_type=event&meta_key=event_date_start&orderby=meta_value_num&order=ASC&posts_per_page=-1');
    foreach ( $myposts as $post ) : setup_postdata( $post );
    
    $today = time();
      if ( $eventnumdays == "Multiple Days" ) {
            $exp_date = strtotime(get_post_meta($post->ID,'event_date_end',true));
      } else {
            $exp_date = strtotime(get_post_meta($post->ID,'event_date_start',true));
      }
    if ($today <= $exp_date) {

    If the custom field for the event_number_of_days is set to Single Day, then there is no custom field created for the end date, so my attempts to use event_date_end failed, which is why I’m using the custom field for number of days instead to determine if the post should expire after the start date or after the end date.

    This query does display the events correctly, just not >>through<< the date they need to be still on the list….

    For example, I have an event going on this weekend – it should show on the list until after midnight on Sunday, since the event goes through Sunday, but since it started on Friday, it already dropped off the list on Friday morning.

    What am I doing wrong?

Viewing 4 replies - 16 through 19 (of 19 total)
  • Sorry, but I can’t find anything wrong with the code. It works as expected when I test it (as much as I can).

    Thread Starter TrishaM

    (@trisham)

    Try using numberposts instead of posts_per_page, no quotes around the -1 since it’s an integer, not a varchar.

    ‘numberposts’ => -1,

    ***EDIT**
    Sorry, just noticed that you’re trying to limit each subgroup (by month) to 8 (?) – what you’re trying to do (above) is limit the TOTAL number of posts to 8 when WP does the initial query, then break that down into Months – that won’t work.

    You’ll need to do an iterative loop……

    FIRST query for ALL events posts using -1 in your args.

    THEN you break it down into mini-foreach loops (foreach $months as $month — be sure to define ‘$months’), using a counter ($i = 1; while( have_posts() && $i < 9 ) { )so that when you get to 8, it stops looping *for that month* and goes to the next month.

    Sorry I can’t give you the full code – I’m not a coder, but that should be enough to either get you going, or for @greencode or another coder to help.

    I’m not trying to limit each subgroup to 8. I am trying to limit all upcoming events to 8.

    I’ve tried ‘numberposts’ and ‘posts_per_page’ but neither work when limiting the amount of posts to a set number i.e. 8

    Thread Starter TrishaM

    (@trisham)

    I don’t think that you can do that, and THEN say “display them categorized by month”…..I think the best you can likely do is to display the 8 posts that get returned (assuming you have at least 8 upcoming events) in a list with the month spelled out first, like so:

    May: 05/01/15 – some cool event
    May: 05/25/15 – another cool event
    June: 06/10/15 – crazy cool event

    That would be no more that calling the ‘date’ of the event twice, and formatting it so that the first instance displays just the month, then a colon, then the second date formatted (however you like it)….

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Query using custom field for event date not working’ is closed to new replies.