stephenharris / Event-Organiser

WordPress plug-in, Event Organiser, development repository

Home Page:http://wordpress.org/extend/plugins/event-organiser/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fields clash with Posts 2 Posts

simonwheatley opened this issue · comments

There is an issue with Events Organiser adding fields to a subquery made by the Posts 2 Posts plugin, this causes a MySQL error.

To replicate the issue:

I setup a connection to go from my "video" post type to an event using the following code:

/**
 * Posts 2 Posts configuration
 * 
 **/
class IR_Posts2Posts {

    /**
     * Singleton stuff.
     * 
     * @access @static
     * 
     * @return IR_Posts2Posts object
     */
    static public function init() {
        static $instance = false;

        if ( ! $instance ) {
            $class = get_called_class();
            $instance = new $class;
        }

        return $instance;

    }

    /**
     * Class constructor
     *
     * @return null
     */
    public function __construct() {
        add_action( 'p2p_init', array( $this, 'action_p2p_init' ) );

        $this->version = 1;
    }

    // HOOKS
    // =====

    /**
     * Hooks the P2P action p2p_init
     *
     * @action p2p_init
     *
     * @return void
     * @author Simon Wheatley
     **/
    public function action_p2p_init() {
        p2p_register_connection_type( array(
            'name'        => 'event_videos',
            'from'        => 'video',
            'to'          => 'event',
            'cardinality' => 'many-to-one',
            'sortable'    => 'any',
        ) );
    }
}


// Initiate the singleton
IR_Posts2Posts::init();

Then I went to a video and created a connection to an event. Then I went to the event and saw:

130621-180441

The problem seems to be the additional fields in the subquery below:

SELECT wp_posts.*, wp_p2p.* 
FROM wp_posts 
INNER JOIN wp_p2p 
LEFT JOIN wp_p2pmeta AS p2pm_order ON ( wp_p2p.p2p_id = p2pm_order.p2p_id AND p2pm_order.meta_key = '_order_to' ) 
WHERE 1=1 
AND wp_posts.post_type IN ('video') 
AND (wp_posts.post_status <> 'trash' 
AND wp_posts.post_status <> 'auto-draft') 
AND (wp_p2p.p2p_type = 'event_videos' 
AND wp_posts.ID = wp_p2p.p2p_from 
AND wp_p2p.p2p_to IN (
    SELECT wp_posts.ID, wp_eo_events.event_id, wp_eo_events.event_id AS occurrence_id, wp_eo_events.StartDate, wp_eo_events.StartTime, wp_eo_events.EndDate, wp_eo_events.FinishTime, wp_eo_events.event_occurrence 
    FROM wp_posts 
    LEFT JOIN wp_eo_events ON wp_posts.id = wp_eo_events.post_id 
    WHERE 1=1 
    AND wp_posts.ID IN (21) 
    AND wp_posts.post_type IN ('event') 
    AND (wp_posts.post_status = 'publish' 
    OR wp_posts.post_status = 'future' 
    OR wp_posts.post_status = 'draft' 
    OR wp_posts.post_status = 'pending' 
    OR wp_posts.post_author = 1 
    AND wp_posts.post_status = 'private') 
    ORDER BY wp_eo_events.StartDate ASC, wp_eo_events.StartTime ASC )
) 
ORDER BY p2pm_order.meta_value+0 ASC, wp_posts.post_date DESC

There is a similar issue reported on the Posts 2 Posts issues here: scribu/wp-posts-to-posts#249. The fix there worked for me.

Yeah, this is related to #98 - the select part of the SQL is ignoring the $query->get( 'fields' ). I'll push an update to GitHub to fix this and #98.

The above fixes resolves this issue for me - could you let me know if it works for you? Should also resolve #98.

Looks good to me, thank you.