0

I want to list the most connected posts ordered by quantity. ¿Is it possible? ¿Could anyone help me with that? I explain with an example: let's say I have Movies custom post type and an Actors custom post type, that are connected with Posts 2 Posts. In the Actors archive, I want to order them by the number of Movies that they have worked on.

I think it is maybe possible with each_connected(), but I don't know how to order the $wp_query array by the number of elements in connected subarray for every $post.

Thanks.

0

I have done this way:

// Find connected pages (for all posts)
p2p_type( 'actors_movies' )->each_connected( $wp_query );

Now we have a $connected property attached to every post on the $wp_query, that is an array of posts connected by actors_movies.

$actors = array();
while (have_posts()) : the_post();
    $movies_acted = count($post->connected);
    if($movies_acted > 0) {
        $actors[$post->ID] = $movies_acted; 
    }
endwhile;
arsort($actors);
foreach ($actors as $id => $quantity) {
    //Show the actor info
}

Then, I have made an array with the ID of the $post as the key, and the quantity of $connected as the value, for each. Then I have ordered it by the value, and I have done a foreach through the array, to get the post from every ID, starting from the most prolific =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.