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
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.
Just to clarify: are you trying to set the standard “post author” of the event or the event owner?
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.
Event owners are actually stored in wp_postmeta along with the post_id. Does that help?
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?
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
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.