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

how to get a post connected with two posts, filtering by two posts #546

Open Orchi- opened 7 years ago

Orchi- commented 7 years ago

hi, i am stuck with using p2p need to get posts which are connected with two other posts.

'connected_type' => array('practice_areas_to_news_articles' , 'attorneys_to_news_articles'); 'connected_items' => array( $_practice_area_id, $_attorney_id);

i need those posts which are connected with both IDs specifically. something like this below, please help

($wpdb->p2p.p2p_type = 'practice_areas_to_news_articles' OR $wpdb->p2p.p2p_type = 'attorneys_to_news_articles') AND $wpdb->posts.ID = $wpdb->p2p.p2p_to AND $wpdb->p2p.p2p_from = '53' AND $wpdb->p2p.p2p_from = '46'

Orchi- commented 7 years ago

using this query for now. but results are weird

SELECT SQL_CALC_FOUND_ROWS
wp_posts., wp_p2p. FROM wp_posts INNER JOIN wp_p2p WHERE 1=1
AND wp_posts.post_type = 'news-articles' AND ((wp_posts.post_status = 'publish')) AND (wp_p2p.p2p_type = 'practice_areas_to_news_articles' OR wp_p2p.p2p_type = 'attorneys_to_news_articles') AND wp_p2p.p2p_from IN (SELECT t1.p2p_to FROM wp_p2p t1 LEFT JOIN wp_p2p t2 ON t2.p2p_to = t1.p2p_to AND t2.p2p_from = '53' WHERE t1.p2p_from = '46') AND wp_posts.ID = wp_p2p.p2p_to ORDER BY wp_posts.post_date DESC LIMIT 0, 30