Okay
  Public Ticket #3348226
Meta Data
Closed

Comments

  •  2
    feriamsidera started the conversation

    I am using elementor loop builder to make a custom events template. I can grab the event name easy enough and link it, but can't find the meta name for the month and separately the date. I am going to display these stacked beside the event name. Do you have a list of the stec meta data that I can pull in using the post meta?

  •  877
    Zhivko replied

    Hi,

    Here's example how you can obtain the meta:

    $event_id               = 1; // Event ID
    $event                  = new \Stachethemes\Stec\Event_Post($event_id); // Event object
    $start_date_full        = $event->get_start_date('Y-m-d H:i:s'); 
    $start_date_month       = $event->get_start_date('m');
    $start_date_month_label = $event->get_start_date('F');
    5283528327.png

    As an alternative just use 

    get_post_meta($event_id, 'start_date', true); // returns Y-m-d H:i:s

    Stachethemes Developer

  •  2
    feriamsidera replied

    Thank you for the response. 

    So is there anyway to add this to my functions.php and have it setup some meta data for all events? So I can pull that meta key into the elementor loop as needed? 

  •  877
    Zhivko replied

    What you could do is to store the values in custom meta upon event save.

    Place the following code in your child-theme functions.php file:

    /**
     * Add custom post meta to event
     * Store event day number, day label and month label in post meta
     * @param int $result - post id
     * @param Event $event
     * @param bool $is_update
     * @return void
     */
    add_action('stec_after_insert_event', function ($result, $event, $is_update) {
        if ( ! is_numeric($result)) {
            return;
        }
        $day_num     = $event->get_start_date('d');
        $day_label   = $event->get_start_date('l');
        $month_label = $event->get_start_date('F');
        update_post_meta($result, 'stec_event_day_num', $day_num);
        update_post_meta($result, 'stec_event_day_label', $day_label);
        update_post_meta($result, 'stec_event_month_label', $month_label);
    }, 10, 3);

    Now when you save/update an event a new meta keys will be added 

    stec_event_day_num
    stec_event_day_label
    stec_event_month_label



    Stachethemes Developer

  •  2
    feriamsidera replied

    You rock man, thanks! This works well. It is okay if not, but anyway you'd be able to abbreviate the month label so everything is 3 characters? 

  •  877
    Zhivko replied

    Sure, replace 

    $day_label   = $event->get_start_date('l');    
    $month_label = $event->get_start_date('F');


    with:

    $day_label   = $event->get_start_date('D');
    $month_label = $event->get_start_date('M');




    Stachethemes Developer

  •  2
    feriamsidera replied

    Okay 1 more question. I can see the link meta but it is an array and when I select that meta for a elementor button, it causes a critical error. So is there a way to just grab the external url from an event so I can use it in a loop grid as needed. Assuming there is a way with some custom php work as you've given before. 

  •  877
    Zhivko replied

    I am afraid you will have to add one additional line to the hook I've sent you earlier:

    update_post_meta($result, 'stec_event_link_url', $event->get_link('url'));

    and then you can use the stec_event_link_url meta key to get the value of the url.

    /**
     * Add custom post meta to event
     * Store event day number, day label, month label and externa link url in post meta
     * @param int $result - post id
     * @param Event $event
     * @param bool $is_update
     * @return void
     */
    add_action('stec_after_insert_event', function ($result, $event, $is_update) {
        if ( ! is_numeric($result)) {
            return;
        }
        $day_num     = $event->get_start_date('d');
        $day_label   = $event->get_start_date('l');
        $month_label = $event->get_start_date('F');
        update_post_meta($result, 'stec_event_day_num', $day_num);
        update_post_meta($result, 'stec_event_day_label', $day_label);
        update_post_meta($result, 'stec_event_month_label', $month_label);
        update_post_meta($result, 'stec_event_link_url', $event->get_link('url'));
    }, 10, 3);


    Stachethemes Developer