Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • bigkinetic

    (@bigkinetic)

    Not sure if this is the best method, but used the following to display the favourite posts for my custom post type (homeswap_listing):

    function homeswap_connection_types() {
    	p2p_register_connection_type( array(
    	    'name' => 'favourite_listing_to_user',
    	    'from' => 'homeswap_listing',
    	    'to' => 'user'
    	) );
    }
    add_action( 'p2p_init', 'homeswap_connection_types' );

    I then call it in the following loop:

    <?php
    	global $userdata,;
    
    	// Get Logged in User ID
    	$userid = $userdata->ID;
    
    	// Setup array of Favourite IDs
    	$fav_id = array();
    
    	// Get posts to call all posts connected to users
    	$fav_posts = get_posts( array(
    			'post_type' => 'homeswap_listing',
    			'connected_type' => 'favourite_listing_to_user',
    			'connected_direction' => 'to',
    			'connected_items' => $user,
    			'suppress_filters' => false,
      			'nopaging' => true
    	) );
    
    	// Loop through posts to get all post IDs where the "p2p_to" values are equal to User IDs
    	// Assign to Favourite ID array
    	foreach($fav_posts as $fav_post){
    		$temp_id = $fav_post->p2p_to;
    		if($temp_id == $userid ){
    			$fav_id[] = $fav_post->p2p_from;
    		}
        }
    
        // Remove duplicates
        $fav_id = array_unique($fav_id);
    
        // New WP Query based on Favourite IDs
    	$args = array(
    		'post_type' => 'homeswap_listing',
    		'post__in' => $fav_id
    	);
    
    	$favourites = new WP_Query( $args );
    
    		// Display connected posts
    		if ( $favourites->have_posts() ) :
    		?>
    		<h3>Favourites:</h3>
    		<ul>
    		<?php while ( $favourites->have_posts() ) : $favourites->the_post(); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endwhile; ?>
    		</ul>
    
            <?php else: ?>
    
    	    <article <?php post_class(); ?>>
    	        <p><?php _e( 'Sorry, no posts matched your criteria.', 'woothemes' ); ?></p>
    	    </article><!-- /.post -->
    
    		<?php
    		// Prevent weirdness
    		wp_reset_postdata();
    
    		endif;
    
    		?>

    Not sure if ‘connected_items’ => $user is correct – if I change $user to ‘user’ it just outputs all the connected posts.

    bigkinetic

    (@bigkinetic)

    Hi Landwire,

    I see you got this to work. I am also trying to add a Google map input to Gravity Forms. Can you give me any advice or point me in the right direction.

Viewing 2 replies - 1 through 2 (of 2 total)