Does anybody know if this is possible with the Posts 2 Posts plugin? Scribu?
Plugin Author
scribu
(@scribu)
It should be possible, but it will require a significant amount of custom code.
Do you think it’s a viable option to use your plugin for what I want to achieve? Or would it be a better option to custom code this from scratch?
Plugin Author
scribu
(@scribu)
Maybe I exagerated when I said “a significant amount”.
With the development version (0.8-beta), you just need to extend the P2P_Box_Multiple class and overwrite the get_query_vars() method:
https://github.com/scribu/wp-posts-to-posts/wiki/Custom-boxes
That’s a lot less code you have to write than starting from scratch.
You’re right, that was tough! 🙂
I did this, at first glance it seems to work. Is this what you were talking about?
class DGO_P2P_Box extends P2P_Box_Multiple {
protected function get_query_vars( $post_id, $page, $search ) {
$query_vars = parent::get_query_vars( $post_id, $page, $search );
// We want to only be able to connect posts that have the 'foo' tag.
$query_vars['connected_from'] = null;
return $query_vars;
}
}
Oh wait, that actually doesn’t work. NVM.
So, this is how I ended up fixing it:
class My_P2P_Box extends P2P_Box_Multiple {
protected function get_query_vars($post_id, $page, $search) {
global $wpdb;
$query_vars = parent::get_query_vars($post_id, $page, $search);
$query = "SELECT $wpdb->p2p.p2p_to AS post_id FROM $wpdb->p2p";
$results = $wpdb->get_results($query);
$r = array();
foreach ($results as $row)
$r[] = $row->post_id;
$query_vars['post__not_in'] = $r;
$query_vars['orderby'] = 'date';
$query_vars['order'] = 'ASC';
return $query_vars;
}
}
p2p_register_connection_type(array(
'from' => 'post',
'to' => 'other_post_type',
'box' => 'My_P2P_Box'
));
For whoever is interested.
Plugin Author
scribu
(@scribu)
Glad to see you figured it out.
Note that you can replace this:
$results = $wpdb->get_results($query);
$r = array();
foreach ($results as $row)
$r[] = $row->post_id;
with this:
$r = $wpdb->get_col($query);