Posts2Posts plugin p2p_query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// new WP_Query( array( 'p2p_query'=>array( array('p2p_type'=>'', 'direction'=>'') ) ) ); | |
// set p2p_type to an existing registered p2p_type | |
// set direction to the direction for queries post type in the connection p2p_type | |
// this will return only items that have connections from the p2p_type | |
add_filter('posts_join', 'mu_p2p_join', 10, 2); | |
add_filter('posts_where', 'my_p2p_where', 10, 2); | |
function my_p2p_join($join, $query) { | |
if (isset($query->query_vars['p2p_query'])) { | |
global $wpdb; | |
foreach ($query->query_vars['p2p_query'] as $i => $p2p_query) { | |
$join .= " INNER JOIN `$wpdb->p2p` AS `p2p_$i` ON (`$wpdb->posts`.`ID` = `p2p_$i`.`{$p2p_query['direction']}`) "; | |
} | |
} | |
return $join; | |
} | |
function my_p2p_where($where, $query) { | |
if (isset($query->query_vars['p2p_query'])) { | |
global $wpdb; | |
foreach ($query->query_vars['p2p_query'] as $i => $p2p_query) { | |
$where .= " AND `p2p_$i`.`p2p_type` = '{$p2p_query['p2p_type']}' "; | |
} | |
} | |
return $where; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment