Support » Plugins » Hacks » Get post meta (date) outside loop

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter Guido

    (@guido07111975)

    I have added a custom meta field called ‘event-days’ to add extra days to variable ‘today’.

    So if an event has date 2016-20-03 + 2 extra days it should be listed in frontend untill 2016-22-03.

    But I’m not on the right track here.. I should NOT add days to strtotime but add days to event date (post meta variable ‘event-date’):

    global $wp_query;
    $postid = $wp_query->post->ID;
    $event_days = get_post_meta( $postid, 'event-days', true );
    $event_date = get_post_meta( $postid, 'event-date', true );
    
    $today = strtotime('today');
    if ($event_days == 'two') {
    	$event_date = strtotime($today . "+1 day");
    } else if ($event_days == 'three') {
    	$event_date = strtotime($today . "+2 days");
    } else if ($event_days == 'four') {
    	$event_date = strtotime($today . "+3 days");
    }

    But still I cannot get the post meta variables.
    So it must be the get_post_meta outside the loop causing this.

    Guido

    Thread Starter Guido

    (@guido07111975)

    No, both examples are no good 🙁

    Main thing is:
    I’m not getting get_post_meta variables outside the loop.

    global $wp_query;
    $postid = $wp_query->post->ID;
    $event_days = get_post_meta( $postid, 'event-days', true );
    $event_date = get_post_meta( $postid, 'event-date', true );

    What am I doing wrong?

    Guido

    Moderator bcworkz

    (@bcworkz)

    Where are you outside the loop? Is this in a widget? The problem outside the loop is the global $wp_query contains the last query run. By the time widgets are output, this can be all sorts of things unrelated to the current main query. Ideally, all intervening code resets their own queries back to the main query, but that’s hardly a given.

    get_queried_object_id() is a handy way to get the current post ID, assuming you’re on a single post page. But it’s still dependent on the global containing the main query.

    To reliably get the correct post ID in a widget, I think you need to get it early and save it in your own global (or class property if you want to play the globals are bad game). For example, hook ‘pre_get_posts’ very late to give other plugins a chance to modify the query, then check that it’s the main query and it’s for a single post. If so, save the ID for later.

    If your widget gets meta based on that saved ID, it should reliably reflect the correct post.

    One other thing. If you are not saving your dates as timestamps, you’re making things very difficult for yourself. As a general rule, convert input to timestamps immediately upon receipt, and leave them as timestamps until the very last moment of output. Do all date related math as timestamps. Querying for date ranges on values that are not timestamps is quite difficult. With timestamps, date range queries are simple.

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    Thanks for your response.

    I want to get the event-date and event-days variable just before the loop. Check this file. You will notice I compare ‘event-date’ with current date ‘today’.

    I want to hook into the ‘event-date’ and add x days to it, depending on the amount of days set in post meta ‘event-days’.

    Guido

    Moderator bcworkz

    (@bcworkz)

    I think I see what you’re trying to do. The problem is you want to dynamically change the date difference for each different record the query checks for compliance against the query vars. There may be a way to do this with a raw SQL query, but not with WP_Query.

    What you really need is an ‘end-date’ meta field that you can query for so the listings found will include multi-day events still running though the event-date start has passed. With the end-date field, you don’t really need the event-days field since it’s implicit in the difference between event-date and end-date. If you do not wish to alter your input form, the end-date field can be calculated from event-date + event-days.

    If you do change the form so the user enters the end-date, this introduces a problem not seen with a calculated end-date. I’m not sure what time is actually stored when you get the date for “today” (Noon?). You may need to subtract a few hours from this value in the query to account for events whose end-dates have technically passed according to the time of day stored, but are in fact still active for several more hours.

    For example, let’s assume the date at noon is the timestamp stored. A summer music festival runs all weekend Fri-Sun. The last show on Sunday runs until 20:00. An organizer would enter Sunday’s date as the end date. If someone were to query for events on Sunday at 13:00, the music festival will no longer show up because its Noon end-date has passed, yet there’s still 7 hours of possible music enjoyment to be had. This situation needs to be accounted for.

    But if the end-date were calculated, the three day event would end up with Monday as the end date. There might need to be several hours adjustment in the other direction!

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    It looks like strtotime(‘today’) has a static timestamp: 1458428400
    This is: Sat, 19 Mar 2016 23:00:00 GMT
    Check this temp link. Guess it’s the same over there? It does the job, no complaints so far.

    I did think about adding an end date, but was not able to build a switch between using begin- or enddate (two meta fields) in the query. That’s why I came up with this (so called) alternative.

    Guido

    Thread Starter Guido

    (@guido07111975)

    Hi again,

    Wait… adding an extra date input for end date isn’t that difficult.

    I change the current date field to end date (so past events stay listed) and add a new field for start date. I then do the query with the end date variabele and with an if statement I decide wheter to show both dates in frontend or just one.

    And once again, it’s so simple when having support from others like you :-)!

    Guido

    Moderator bcworkz

    (@bcworkz)

    Hah! Very clever. Nice work.

    No one person can have all the ideas, it takes a community. And there’s very few communities as awesome as the WP community!

    And yes, the timestamp is the same for me using your link naturally, but the same code on my server returns 1458432000 = Sun, 20 Mar 2016 00:00:00 +0000 UTC because it is not adjusting for time zone nor the fact the US is already in daylight/summer time. I believe your zone is Central Europe +1 UTC? So ‘today’ for you begins at your local midnight, for me it starts locally 17:00 the day before due to a different server configuration.

    I vaguely recall intentionally setting my server to use UTC because I think all this timezone nonsense in a global economy is needless confusion. Don’t get me started on daylight/summer time!

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    It’s now midnight and an event from yesterday is still listed. I guess it’s not listed anymore within 1 hour. Yeah, we have UTC+1 over here.

    I have updated plugin, using the end date for the query. Check this. I only show start date in frontend if this is before or the same as end date. It’s simple but does the trick.

    Topic solved!

    Guido

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    Found out today WP has it’s own function to get the ‘current time’ set in Settings > General:

    current_time( 'timestamp' );

    Guido

    Moderator bcworkz

    (@bcworkz)

    😀 I don’t think anyone can claim to know all of the functions available in WordPress. Always something to learn! As you likely noticed, current_time() applies the time zone adjustment that is specified in Settings.

    Cheers

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Get post meta (date) outside loop’ is closed to new replies.