wp-posts-to-posts icon indicating copy to clipboard operation
wp-posts-to-posts copied to clipboard

Is there a method to get posts that have at least one connection of a given connection type?

Open l3rady opened this issue 11 years ago • 1 comments

I'm looking to query a post type but I dont want to return posts that don't have a connection to another post.

Is it possible?

l3rady avatar Oct 02 '14 09:10 l3rady

Scribu suggested the answer is "no" two years ago: https://wordpress.org/support/topic/connected_to-any-returning-duplicates

And suggests re-indexing by id to remove duplicates.

I feel there is, or there should be, a better way to do this. Right now, I'm doing this with get_posts like this (kinda ugly):

//get all instructors that have course connections $instructors = get_posts( array( 'supress_filters' => false, 'post_type' => 'person', 'posts_per_page'=>-1, 'connected_type'=>'course_to_instructor', 'connected_direction'=>'from', 'connected_items'=>'any', 'orderby'=>'title', 'order'=>ASC, ) ); //remove duplicates $instructors = array_reduce($instructors, function($acc, $curr){ $acc[$curr->ID] = $curr; return $acc; }, array());

davidchoy avatar Mar 27 '15 18:03 davidchoy