• Resolved Daedalon

    (@daedalon)


    We have a PHP script that creates new $EM_Event objects and saves them. However, we noticed that despite changing the author fields of the $EM_Event manually, $EM_Event->save will save the current user as the author of the event.

    Is there a reason for this, or could EM start respecting either $EM_Event->owner or $EM_Event->event_owner when saving an event, so our own code can decide the owner without an additional SQL query?

    https://wordpress.org/plugins/events-manager/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Philip John

    (@philipjohn)

    Hiya,

    How, exactly are you setting the author?

    Check that the data you are passing to $EM_Event is in the format that EM_Event expects first of all.

    You should then debug exactly what EM_Event does with that.

    Thanks

    Thread Starter Daedalon

    (@daedalon)

    How, exactly are you setting the author?

    $event_owner                = '123';
    $event_name                 = 'Example event';
    $EM_Event                   = new EM_Event();
    $EM_Event->event_owner      = $event_owner;
    $EM_Event->owner            = $event_owner;
    $EM_Event->event_name       = $event_name;
    //etc.
    $EM_Event->save();

    Everything else works perfectly and the event gets saved with on the information set, but its owner gets changed to the user who ran the script.

    the format that EM_Event expects first of all.

    Is there a description of the EM_Event’s structure somewhere that I’ve missed? It would come handy in many occasions. Definitions of other objects would help as well.

    caimin_nwl

    (@caimin_nwl)

    Just to clarify: are you trying to set the standard “post author” of the event or the event owner?

    Thread Starter Daedalon

    (@daedalon)

    Is there a difference? If, then both.

    I’ve only checked the em_events database table’s event_owner column so far. It gets set as the user who runs the script that saves these new events. We want to specify the user ID to be used for events saved by the script.

    caimin_nwl

    (@caimin_nwl)

    Event owners are actually stored in wp_postmeta along with the post_id. Does that help?

    Thread Starter Daedalon

    (@daedalon)

    That’s good to know, just in case. The original issue still stands: we’re looking for the best way to set the author. Can it be set when creating an event, or is the only possible way to use a separate SQL query afterwards?

    Philip John

    (@philipjohn)

    Have you checked how EM sets the post author field? You should be able to either hook in at the same place, or follow the same method.

    Thanks

    Thread Starter Daedalon

    (@daedalon)

    Got this working with the following:

    if ( $EM_Event->save() ) {
        $post_id = $EM_Event->post_id;
        // EM_Event->save ignores event owner fields, so set them manually
        $wpdb->update(
            $wpdb->prefix . 'posts',
            array( 'post_author' => $event_owner ),
            array( 'ID' => $post_id )
        );
        $wpdb->update(
            EM_EVENTS_TABLE,
            array( 'event_owner' => $event_owner ),
            array( 'post_id' => $post_id )
        );
    }

    Hopefully this helps those tackling the same issue.

    An even better way might have been to hook into do_action('em_event_save_pre', $this); from line 637 from classes/em-event.php, but not enough time for perfecting this. Using that action could improve performance slightly by saving two database updates. It’s odd that EM_Event->save ignores the event_owner and owner fields, but perhaps this doesn’t affect enough users that it’s worth sweating over.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Change author of new event in PHP’ is closed to new replies.