Using eo_insert_event with custom fields

WordPress Event Management, Calendars & Registration Forums General Question Using eo_insert_event with custom fields

This topic contains 12 replies, has 3 voices, and was last updated by  Stephen Harris 8 years, 3 months ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #10710

    How would I got about adding custom fields to the eo_insert_event args? Do I need to just call update_meta on the post once it is created?

    Thanks

    Gabriel David
    #10711

    Actually, I would like to make this topic a bit more broad; let’s say I also need to create a new venue and category at the same exact time. How would I go about doing that?

    Gabriel David
    #10713

    Sorry to add more, but how would I also set up tickets using eo_insert_event? I just need a general, free, 1 space ticket.

    Gabriel David
    #10727

    How would I got about adding custom fields to the eo_insert_event args? Do I need to just call update_meta on the post once it is created?

    Yes, just use the returned event (post) ID and then use update_post_meta() / add_post_meta().

    For venues, I would first create the event, then create the venue/category and then assign the category/venue to the event:

    • use eo_insert_venue() to create a venue
    • use wp_insert_term() to create a category
    • use wp_set_post_terms() to assign venue/category (taxonomy: event-venue, event-category respectively) to assign a category/venue to an event.

    The event ticket API is not yet public – this just means that it could change in the future without maintaining backwards compatibility. However, there have never been any ‘breaking changes’ and there are no plans to do so. But you can insert/update a ticket for an event with eventorganiser_insert_ticket() and eventorganiser_update_ticket(). There is no documentation of these functions outside the source code (but there are doc-blocks). You can find tem in includes/tickets.php.

    I’ll just mention here that the occurrence_ids for tickets is only required if selling by date. To get all the occurrence IDs of an event, see this thread: http://wp-event-organiser.com/forums/topic/get-occurrence-id/

    Stephen Harris
    #10749

    Awesome, thanks! That’s about how I would go about it but wanted to make sure there wasn’t a better/easier way.

    Gabriel David
    #10751

    I actually don’t seem to see the file tickets.php. Is it new in the 1.7 structure? Where can I download 1.7?

    Gabriel David
    #10752

    No, it’s in 1.6.4 too. wp-content/event-organiser-pro/includes/tickets.php. There is also booking-tickets.php in the same folder, but that isn’t the file I’m referring to.

    Stephen Harris
    #10761

    Ah, I’m in event-organiser/

    Thanks!

    Gabriel David
    #25087

    Hi.

    I am having a problem using ‘wp_set_post_terms’ when using ‘eo_insert_event’.

    I’ve written a function which imports events from a json feed and uses add_post_meta to add data to custom fields. eo_insert_event is working well to create events, so my last thing to do is to to use the feed’s category to populate the event-category of the created event.

    Here’s what I’ve got. If you can see anything i’m missing please let me know.

    Thanks

        <?php
    function create_events() {
    
    $slices = json_decode(file_get_contents('https://feed.feed.co.uk/embed/calendar/json'),true);
    
    $post_id = -1;
    
    if ($slices) { 
        foreach ($slices as $slice) {
            /* Grab info from feed */
            $title = $slice['name'];
            $content = $slice['description'];
            $churchapp_id = $slice['id'];
            $event_start = $slice['datetime_start'];
            $event_end = $slice['datetime_end'];
            /**/
            $event_images = $slice["images"];
                $event_image_feat = $event_images['original_1000'];
            /**/
            $event_location = $slice['location'];
                $event_location_name = $event_location['name'];
                $event_location_address = $event_location['address'];
            /**/
            $event_cat = $slice["category"];
                $event_cat_name = $event_cat["name"];
    
            /* Format Event Content */
            $content_html = html_entity_decode( $content );
            /* Format Event Category */
            $event_cat_name_lower = strtolower($event_cat_name);
    
            // Create post object
            $post_data = array(
                'post_title'    =>  $title,
                'post_content'  => $content_html,
                'post_status'   => 'publish'
            );
    
            $event_data = array(
                'start'    =>  new DateTime($event_start, eo_get_blog_timezone() ),
                'end'    => new DateTime($event_end, eo_get_blog_timezone() ),
            );
    
            // Check that event doesn't already exist
            $churchapp_ids = array();
    
                $args = array(
                    'posts_per_page' => -1,
                    'order' => 'ASC',
                    'orderby' => 'eventstart',
                    'post_type' => 'event',
                );
                $the_query = new WP_Query( $args );
                while ( $the_query->have_posts() ) : $the_query->the_post();
    
                    $existing_churchapp_id = get_post_meta(get_the_id(), '_events_id', 1);
    
                    array_push($churchapp_ids, $existing_churchapp_id);
    
                endwhile; wp_reset_query();
    
    
            if(in_array($churchapp_id,$churchapp_ids)) { // update posts
    
    
            } else { // create new Events
    
            $post_id = eo_insert_event( $post_data, $event_data );
    
            wp_set_object_terms( $post_id, $event_cat_name_lower, 'event-category', true);
    
            // Add custom field meta
            add_post_meta( $post_id, '_events_id', $churchapp_id ); 
            add_post_meta( $post_id, '_events_image', $event_image_feat );
            add_post_meta( $post_id, '_events_location_name', $event_location_name );
            add_post_meta( $post_id, '_events_location_address', $event_location_address );
    
                } // else end
            } // foreach end
        } // if slices end
    }
    
            add_filter( 'after_setup_theme', 'do_this_hourly' );
            ?>
    
    Nathan Skelton
    #25089

    Are you sure that $event_cat_name_lower is the slug of an existing category? It all looks fine to me. eo_insert_event will return a post ID and then you’re using wp_set_object_terms() (seemingly) correctly.

    Stephen Harris
    #25101

    I have echoed $event_cat_name_lower and it is returning a string. I’ve tried just entering a string and ID in the function too – wp_set_object_terms( $post_id, 'events', 'event-category', true);, but that is not working.

    I have also tried wp_set_post_terms, and that also isn’t working…

    Does the order of everything else look ok to you?

    Nathan Skelton
    #25102

    Ok, got it working now using add_action( 'init', 'create_events', 99 ); instead of add_filter. So that’s good news.

    I am now working on the ‘if’ part of the argument updating the events if they already exist. I’m trying the below, but that is just timing out. Any ideas how to get this to work? I really appreciate your input.

    Thanks

    if(in_array($churchapp_id,$churchapp_ids)) { // update events
    
                $post_id = eo_update_event( $post_data, $event_data );
    
                //wp_set_object_terms( $post_id, $event_cat_name_lower, 'event-category');  
    
     } else { // create new events
    
                $post_id = eo_insert_event( $post_data, $event_data );
    
                wp_set_object_terms( $post_id, $event_cat_name_lower, 'event-category');
    
                // Add custom field meta
                add_post_meta( $post_id, '_events_id', $churchapp_id ); 
                add_post_meta( $post_id, '_events_image', $event_image_feat );
                add_post_meta( $post_id, '_events_location_name', $event_location_name );
                add_post_meta( $post_id, '_events_location_address', $event_location_address );
    
    
    } // else end
    
    Nathan Skelton
    #25116

    For each event in the feed you are querying all existing events to see if that event has already been imported. You could save some memory by just doing the query once, before you start parsing the feed.

    Alternatively you could perform a meta-data based query to check if the given source ID exists in the meta data tables. (It might be more efficient to write your own SQL for this).

    The iCal sync plugin does this (see eo_fetch_feed() and eo_get_event_by_uid() in ical-functions.php).

    Better still, but requiring a bit more effect would be have to have a custom table mapping the ID of the event in the source to the ID of the event in WordPress. This would allow for a quicker look up.

    Stephen Harris
Viewing 13 posts - 1 through 13 (of 13 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.