Thread Starter
sdls
(@simon_said)
Thanks to the filter added by the Author it was pretty fast to make this happen…..perhaps this will be useful to others.
function restrict_posts_by_relation() {
global $typenow;
$post_type = 'yourfirstposttype'; // change HERE
$related_post = 'yoursecondposttype'; // change HERE
$related_post_drop_down = 'related_'. $related_post. '_post';
if ($typenow == $post_type) {
// $selected = isset($_GET[$related_post]) ? $_GET[$related_post] : '';
// create the drop down of related posts
$args = array(
'orderby'=>'title',
'order' => 'ASC',
'showposts' => '-1',
'post_type' => $related_post,
);
$dropposts = new WP_Query($args);
if( $dropposts->have_posts() ):
?>
<select id="<?php echo $related_post_drop_down; ?>" class="postform" name="<?php echo $related_post_drop_down; ?>">
<?php
while( $dropposts->have_posts() ): $dropposts->the_post();
// setup_postdata($post);
?>
<option class="level-1" value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php
endwhile;
?>
</select>
<?php
endif;
wp_reset_postdata();
};
}
add_action('restrict_manage_posts', 'restrict_posts_by_relation');
function convert_relation_to_query($query) {
global $pagenow;
$post_type = 'yourfirstposttype'; // change HERE
$related_post = 'yoursecondposttype'; // change HERE
$related_post_drop_down = 'related_'. $related_post. '_post';
$connection_reference = 'youronnectionreference';
// change HERE
$related_post_ID = $_GET[$related_post_drop_down];
$q_vars = &$query->query_vars;
if ($pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($related_post_ID) && is_numeric($related_post_ID) && $related_post_ID != 0) {
// echo "beep";
$mod_post = get_post( $related_post_ID );
// print_r($mod_post);
$args = array(
'connected_type' => $connection_reference,
'connected_items' => $mod_post,
);
query_posts($args);
}
}
add_filter('parse_query', 'convert_relation_to_query');
Thread Starter
sdls
(@simon_said)
P.S. This code is not perfect and still needs some love
Plugin Author
scribu
(@scribu)
Thanks for sharing, sdls. This is something that should be included in P2P eventually: https://github.com/scribu/wp-posts-to-posts/issues/115
I’ve copied your code to a gist in case it gets moderated.
Plugin Author
scribu
(@scribu)
This is now implemented in the development version (1.5-alpha). Just add the following line to your p2p_register_connection_type() args:
'admin_dropdown' => 'any'
Thank you scribu for integrating this !
I now use your plugin a lot 🙂
Though, I now see 2 enhancements which would be great added features :
– Support for several query filters at the same time.
So far, only the 1st used filter is taken into account
– Add automatically the selected parameter to the dropdown when a filter is applied from a post item P2P admin column (and therefore not from the dropdown list).