Creating Event Archive

Home Forums Calendar Products Events Calendar PRO Creating Event Archive

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #198386
    James
    Participant

    Hey there. I’ve emailed about this topic a few times, and I keep thinking I’m making some headway… then… well, I haven’t.

    I’m trying to create a custom query to return only events that happen in a given (past) year.

    I’ve read a lot and found the basis for this solution here https://theeventscalendar.com/queries-and-pagination/

    But it’s just not going well.

    The code is here: https://gist.github.com/jamesdidit/6bd1a316ab8da0a755ff

    What I did was try to create a custom query wherein if the variable $searchyear has been defined in the URL, then it will use a custom query wherein the meta field “event_year” — which is a custom field I’ve added using Pods — will be queried.

    Please help.

    This is now moved to http://fwa.org/event-archive/past-events/

    #199492
    Barry
    Member

    Hi!

    How about a different approach where you set up a filter using the parse_query hook and then, according to the query variables in the URL, modify the start_date and end_date properties of the query? It’s very much customization territory so we can’t offer too much help for something like this, but is probably the cleanest way to handle this (and still enjoy the benefits of pagination etc).

    Some of our own logic uses that very same hook (see TribeEventsQuery::parse_query() which runs at priority 50, so you would probably wish to use a higher priority for your own callback) if you want to get a sense of what’s happening “under the hood”.

    #201071
    James
    Participant

    Thank you. I understand that you’re limited in how you can help.
    Here’s what I’ve tried… any help is appreciated:

    add_filter (‘parse_query’,’fwa_query_post_year’,”,1);
    function fwa_query_post_year ($searchyear) {
    if (!empty($searchyear) && $query[‘post_type’]==’tribe_events’) {
    $query->set(‘start_date’,$searchyear);
    $query->set(‘end_date’,$searchyear);
    }
    return $query;
    }

    I also tried _EventStartDate and _EventEndDate

    I think there’s something with the conditional, but when I remove the second half, it breaks the whole site.

    Any help is GREATLY appreciated.

    #201112
    Barry
    Member

    OK, so unless I’m misreading fwa_query_post_year() is your callback for the parse_query hook.

    By default it’s going to receive a reference to the WP_Query object, so when you set the start date and end date to $searchyear you are really setting them to the WP_Query object rather than a specific date.

    What you probably want to do is draw $searchyear from the URL query and – after sanitizing it etc – expand it from something like “2014” to a matching pair of start and end dates like “2014-01-01 00:00:00” and “2014-12-31 23:59:59” (to encompass the whole of 2014).

    Does that help?

    #206312
    James
    Participant

    That was very helpful because now uploading the function doesn’t break my whole site, but it also doesn’t do anything when I define the year in the URL.

    In my functions.php file…
    // Events archive
    global $searchyear;
    $searchyear = NULL;
    add_filter (‘parse_query’,’fwa_query_post_year’);
    function fwa_query_post_year ($query) {
    $searchyear = $_GET[“searchyear”];
    $newstartdate = $searchyear.’-01-01 00:00:00′;
    $newenddate = $searchyear.’-12-31 23:59:59′;
    //$eventposttype = $query[‘post_type’];
    if (!empty($searchyear) && tribe_is_event_query()) {
    $query->set(‘_EventStartDate’,$newstartdate);
    $query->set(‘_EventEndDate’,$newenddate);
    }
    return $query;
    }

    #207539
    Barry
    Member

    Hmm, well one thing that leaps out is you aren’t specifically setting the priority for your callback.

    As covered in my first reply, our own parse_query callback is set to a priority of 50 (whereas the default priority is 10 – so our callback will run after yours … which may mean your modifications to the query are being overwritten).

    I’d love to help more but really this is beyond the level of support we can offer. I do wish you luck but I’m afraid at this point I’ll have to go ahead and close out this thread.

    If we can help with any other issues, though, please do feel free to create new threads as needed.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Creating Event Archive’ is closed to new replies.