scribu / wp-posts-to-posts

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

Posts 2 Posts and Polylang plugin problems #575

Open mklusak opened 3 years ago

mklusak commented 3 years ago

Hi, I am not looking for advice, I just want to share one thing that can be useful for someone else (and googable). So - my site uses P2P plugin for many years, connecting "posts" to "locations". Recenty I installed Polylang plugin for multilingual WordPress, and started translating posts and locations to other languages. And a strange thing happened. In backend (and it was similar on frontend) everything worked when I was viewing "all languages" items lists (posts list). But switching to default EN language caused some "locations" missing - its column was empty. It was not language issue - those missing locations were set to EN language. Switching languages to "show all languages" filled all columns with location names, again.

Long story short, the issue is - when connecting "locations" to posts, and "locations" are multilingual post type by Polylang, then Polylang alters WP_Query with its tax_query (languages etc. are handled as taxonomy and terms by Polylang). The problem is, when WP_Query contains tax_query, WordPress adds "GROUP BY wp_posts.ID" to final SQL statement. And this grouping is something P2P does not expect.

For example - I have 10 posts that are all connected to one Location A. P2P then gets 10 results from its WP_Query, and all the rows are identical/duplicate "Location A" posts (with same wp_posts.ID, obviously), and every one of them has its "p2p connection id" column with link to its post item. But if Polylang steps in, the result set is grouped by wp_posts.ID, and returns not 10 duplicate rows/locations, but only one. So P2P plugin has now only one location for one post, and remaining 9 posts have "location column" empty.

This issue is not limited to Polylang, sure. It happens everytime any WP_Query with P2P connections is tax_queried, or has meta_key / meta_query added ... everytime GROUP BY addition takes place, respectively. After long hours of debugging and googling (and not finding anything) this old issue #304 revealed what could be wrong with my website.

My solution (but I am not sure if it works 100% correctly) is to hook to posts_groupby filter and reseting the group by statement for P2P WP_Queries ... but how to tell if query is P2P query? I noticed its queries have some query vars added, connected_type for example. So I am checking against this query var. It fixed all my issues, and looks like it did not break anything else on my website (I hope).

functionp2p_posts2posts_groupby( $groupby, $wp_query ) {

    if( !empty($wp_query->query_vars['connected_type']) ) {
        return '';
    }

    return $groupby;
}
add_filter('posts_groupby', 'p2p_posts2posts_groupby', 20, 2);

So hopefully it will save someone a few hours of wondering what could be wrong with their website / theme :-)