Skip to content

Instantly share code, notes, and snippets.

@scribu
Created November 26, 2012 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scribu/4150626 to your computer and use it in GitHub Desktop.
Save scribu/4150626 to your computer and use it in GitHub Desktop.
P2P Admin Dropdown
# author: sdls
# source: http://wordpress.org/support/topic/p2p-admin-view-sort-by-drop-down#post-3446538
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment