scribu / wp-posts-to-posts

Efficient many-to-many connections between posts, pages, custom post types, users.
http://wordpress.org/plugins/posts-to-posts/
975 stars 261 forks source link

Unexpected conflict with custom queries #249

Open codearachnid opened 12 years ago

codearachnid commented 12 years ago

I ran into an interesting DB error:

WordPress database error: [Operand should contain 1 column(s)]

upon further review it turns out that the bug was being created by another plugin, Tri.be's The Event Calendar (side note: which I'm a dev for). We are doing a major overhaul of our plugin's query structure that will fix this issue down the line. In the interest of suggesting a clean way to prevent this from happening to older versions of our plugin or other plugins that might be cluttering WP_Query with custom fields, I submit this simple filter to ensure the nested select only has 1 column.

<?php

/**
 * Fix compatibility issues with The Event Calendar and Posts 2 Posts WordPress Plugins
 * Issue arrises with how nested select queries function when P2P expects a single column.
 */

function tribe_to_p2p_pre_get_posts( $query ){
    if(isset($query->_p2p_capture) && $query->_p2p_capture) {
        add_filter( 'posts_fields', 'tribe_to_p2p_setupFields', 20);    
    } else {
        remove_filter( 'posts_fields', 'tribe_to_p2p_setupFields', 20); 
    }
    return $query;
}
function tribe_to_p2p_setupFields( $fields ){
    global $wpdb;
    $fields = "{$wpdb->posts}.ID";
    return $fields;
}
add_action( 'pre_get_posts', 'tribe_to_p2p_pre_get_posts');

Source: https://gist.github.com/3815849

sstasio commented 12 years ago

Thanks for this solution, it was driving me crazy trying to figure this out.

codearachnid commented 12 years ago

Glad it was able to help you out.