WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Admin View Sort by Drop Down (6 posts)

  1. sdls
    Member
    Posted 1 year ago #

    Firstly, AWESOME PLUG-IN!

    Quick questions, I notice there is support for listing the p2p relation in the admin view (/wp-admin/edit.php).. works a charm.

    Do you have any suggestions as to how I could expand this to include a drop down filter at the top of the page with a list of all related posts, enabling filter by related post?

    Any feedback would be greatly appreciated.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. sdls
    Member
    Posted 1 year ago #

    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');
  3. sdls
    Member
    Posted 1 year ago #

    P.S. This code is not perfect and still needs some love

  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  5. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    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'
  6. antoinicolas
    Member
    Posted 1 year ago #

    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).

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic