Support » Plugin: Posts 2 Posts » connected_to => 'any' returns duplicates

  • Resolved jazbek

    (@jazbek)


    I have 2 custom post types: schedule_item and venue. There is a many-to-one connection from schedule_item to venue:

    p2p_register_connection_type( array(
    	'name' => 'scheduleitem_to_venue',
    	'from' => 'schedule_item',
    	'to' => 'venue',
    	'cardinality' => 'many-to-one',
    	'title' => array('to' => __('Schedule Items'), 'from' => __('Venue')),
    	'fields' => array(
    		'screen' => array(
    			'title' => 'Screen',
    			'type' => 'text',
    		),
    	),
    ) );

    There exists in the database 98 schedule_item_to_venue connections, but there are only 14 venues.

    I’d like to get a list of all venues that have at least one schedule item connected to them, but when I do:

    $venues = get_posts(array(
    	'post_type' => 'venue',
    	'posts_per_page' => -1,
    	'orderby' => 'title',
    	'order' => 'asc',
    	'update_post_term_cache' => false,
    	'update_post_meta_cache' => false,
    	'connected_type' => 'scheduleitem_to_venue',
    	'connected_direction' => 'from',
    	'connected_items' => 'any',
    ));

    I am getting a list of 98 venues, with most being duplicates. How can I get a unique list of venues that have at least one connected schedule item?

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author scribu

    (@scribu)

    One way to eliminate the duplicates is to re-index the posts by their ID:

    $connections = new WP_Query( array(
    	'post_type' => 'venue',
    	'posts_per_page' => -1,
    	'orderby' => 'title',
    	'order' => 'asc',
    	'update_post_term_cache' => false,
    	'update_post_meta_cache' => false,
    	'connected_type' => 'scheduleitem_to_venue',
    	'connected_direction' => 'from',
    	'connected_items' => 'any',
    ) );
    
    $venues = array();
    
    foreach ( $connections->posts as $post ) {
      $venues[ $post->ID ] = $post;
    }

    Now just loop through $venues or use p2p_list_posts( $venues );

    How to integrate this with pagination?

    Example:

    <?php
    	$id = 'any'; if( $_GET['filter']!='' ) $id = $_GET['filter'];
    	$query = new WP_Query( array(
    		'post_type' => 'portfolio',
    		'connected_type' => 'portfolio_to_page',
    		'connected_direction' => 'to',
    		'connected_items' => $id,
    		'orderby' => 'post_date',
    		'order' => 'DESC',
    		'post_status' => 'publish',
    		'nopaging' => false,
    		'posts_per_page' => 9,
    	) );
    
    	p2p_type( 'portfolio_to_page' )->each_connected( $query, array(), 'page' );
    
    ?>
    
    <?php if ( $query->have_posts() ) : $used = array(); ?>
    
    	<?php while ( $query->have_posts() ) : $query->the_post(); if( in_array( get_the_ID(), $used ) ) continue; else $used[] = get_the_ID(); ?>
    		<?php get_template_part( 'content', 'portfolio' ); ?>
    	<?php endwhile; ?>
    
    <?php endif; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘connected_to => 'any' returns duplicates’ is closed to new replies.