Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields clash with Posts 2 Posts #99

Closed
simonwheatley opened this issue Jun 21, 2013 · 3 comments
Closed

Fields clash with Posts 2 Posts #99

simonwheatley opened this issue Jun 21, 2013 · 3 comments

Comments

@simonwheatley
Copy link

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.

@stephenharris
Copy link
Owner

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.

stephenharris added a commit that referenced this issue Jun 21, 2013
… columns when we're only after one ID/parent ID columns. Append, rather than prepend event columns. Thanks to @simonwheatley. Ref #98, Ref #99
@stephenharris
Copy link
Owner

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

@simonwheatley
Copy link
Author

Looks good to me, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants