WordPress.org

Forums

Posts 2 Posts
[resolved] Limit connection to one post? (3 posts)

  1. Jon Brown
    Member
    Posted 2 years ago #

    Two questions really:
    1) Can the connection be setup to limit the connection to a single post?

    2) Is there an efficient way to query a singular connected post and return it rather than a full $query.

    FWIW, I'm connecting a CPT to a post and pages to be used as a featured/hero image.

    Currently I'm using this, but thinking there might be a better way:

    <?php
    function jb_feature_area() {
    
    	// Find connected pages
    	$connected = new WP_Query( array(
    	  'connected_type' => 'posts_to_whitepaper',
    	  'connected_items' => get_queried_object(),
    	  'nopaging' => true,
    	) );
    
    	// Display connected pages
    	if ( $connected->have_posts() ) {
    #		while ( $connected->have_posts() ) : $connected->the_post();
    			$connected->the_post();
    			$post_thumbnail_id = get_post_thumbnail_id();
    			$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
    
    			echo '<div id="page-feature" class="page-feature" style="background-image: url(' . $post_thumbnail_url . ')">';
    			echo '<div class="feature-excerpt-wrap">';
    			echo '<h2>' . get_the_title() . '</h2>';
    			echo '<p>' . get_the_excerpt() . '</p>';
    			echo '</div> <!-- end .feature-excerpt-wrap -->';
    			echo '</div> <!-- end .home-feature -->';
    #		endwhile;
    	}
    
    	// Prevent weirdness
    	wp_reset_postdata();
    }

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

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    1) Can the connection be setup to limit the connection to a single post?

    Yes, use the 'cardinality' setting: https://github.com/scribu/wp-posts-to-posts/wiki/Duplicate-connections-and-cardinality

    2) Is there an efficient way to query a singular connected post and return it rather than a full $query.

    No, not really.

  3. Jon Brown
    Member
    Posted 2 years ago #

    Thanks! Sometimes the obvious escapes me... I'm 0/2 on this...

    1) Exactly what I was looking for, I totally missed in the wiki in spite of seeing the entries above and below it (I went back to look because I didn't believe it was there)

    2) Duh... just realized I can restrict it to return one post with post_count in the WP_query which will do what I need and later I might randomly pick one of multiple connections sop this is good anyway).

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Posts 2 Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.