WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Display pages only for connected user (4 posts)

  1. axeldev
    Member
    Posted 1 year ago #

    Hello,

    First of all thank you scribu for an awesome plugin!

    I've looked at the wiki and browsed through support topics but I can't seem to get this to work.

    I am using Posts 2 Users and managed to connect users to pages where they are linked to category: presentation. When a user logs in, I want them to see a list of pages they are connected to.

    So far everything seems to work fine, except all users see the same list when they login. I am unable to filter according to the own user's connections. What am I doing wrong?

    function my_connection_types() {
    	p2p_register_connection_type( array(
    		'name' => 'multiple_authors',
    		'from' => 'page',
    		'to' => 'user',
    	) );}

    I set this up to work only with my showcase template:

    p2p_register_connection_type( array(
    		'id' => 'landing2page',
    		'from' => 'page',
    		'from_query_vars' => array(
    			'meta_key' => '_wp_page_template',
    			'meta_value' => 'showcase.php'
    		),
    		'to' => 'user',
    		'title' => 'Connect Users'
    	) );

    I'm using a shortcode called [mypresentations]:

    function get_mypresentations() {
    // Find connected posts
    $connected = new WP_Query( array(
      'connected_type' => 'landing2page',
      'connected_items' => $user,
      'suppress_filters' => false,
      'nopaging' => true,
      'category' => 'presentation',
    ) );
    
    // Display connected posts
    if ( $connected->have_posts() ) :
    ?>
    <h3>Related posts:</h3>
    <ul>
    <?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    
    <?php
    // Prevent weirdness
    wp_reset_postdata();
    
    endif;
    }
    add_shortcode( 'mypresentations', 'get_mypresentations' );

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

  2. axeldev
    Member
    Posted 1 year ago #

    Hello, I was wondering if anyone managed to display pages connected to a single user? I still can't get this to work on my end...

    Many thanks

  3. bigkinetic
    Member
    Posted 1 year ago #

    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.

  4. axeldev
    Member
    Posted 1 year ago #

    Hello bigkinetic,

    Thanks so much for your help!! Really appreciate it. You definitely put me on the right track: the right pages are filtering correctly for the connected user.

    To make it work I used 'connected_items' => $query->user->ID instead of '$user'.

    The only issue I'm still facing is for a user that has no connection: it outputs a number of pages - always the same - instead of the message ('Sorry...').

    Is the issue to do with connecting pages rather than a custom post type?

    function my_connection_types() {
    p2p_register_connection_type( array(
    		'name' => 'landing2page',
    		'from' => 'page',
    		'to' => 'user',
    		'title' => 'Connect Users'
    	) );
    }
    add_action( 'p2p_init', 'my_connection_types' );
    function get_mypresentations() {
    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' => 'page',
    'connected_type' => 'landing2page',
    'connected_direction' => 'to',
    'connected_items' => $query->user->ID,
    'suppress_filters' => false,
    'nopaging' => true,
    'category' => 'presentation',
    ) );
    // 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' => 'page',
    'category' => 'presentation',
    '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>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    
     <?php else: ?>
    
    	    <article <?php post_class(); ?>>
    	        <p><?php _e( 'Sorry, no posts matched your criteria.', 'twentyeleven' ); ?></p>
    	    </article><!-- /.post -->
    
    <?php
    // Prevent weirdness
    wp_reset_postdata();
    
    endif;
    }
    add_shortcode( 'mypresentations', 'get_mypresentations' );

    Thank you so much again for your support so far!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic