WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Exclusive relations (er, monogamy? ;-) ) (9 posts)

  1. waltervos
    Member
    Posted 3 years ago #

    Hi,

    Is it possible to create an exclusive relationship between a post and one or more other posts (post here can mean any instance of a post type)? What I mean is that once I've connected Post A to Other_post X and Other_post Y I should no longer be able to connect Other_post X and Other_post Y to Post B. Can this be accomplished by the plugin?

    - Walter

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

  2. waltervos
    Member
    Posted 3 years ago #

    Does anybody know if this is possible with the Posts 2 Posts plugin? Scribu?

  3. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    It should be possible, but it will require a significant amount of custom code.

  4. waltervos
    Member
    Posted 3 years ago #

    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?

  5. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  6. waltervos
    Member
    Posted 3 years ago #

    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;
    		}
    	}
  7. waltervos
    Member
    Posted 3 years ago #

    Oh wait, that actually doesn't work. NVM.

  8. waltervos
    Member
    Posted 3 years ago #

    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.

  9. scribu
    Member
    Plugin Author

    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic