[wp-hackers] Time for a wp_post_relationships table?

scribu scribu at gmail.com
Wed Aug 4 00:03:27 UTC 2010


On Wed, Aug 4, 2010 at 1:42 AM, Mike Schinkel
<mikeschinkel at newclarity.net>wrote:

> Can you show some proof-of-concept SQL so I can see what you are intending?
>  Show queries for 1.) actors in a given movie, and 2.) movies that the actor
> acted in, please?
>

I think one of the reasons you're getting hung up on this is that you're
trying to do it all in one giant query, instead of using the available APIs.

Here's how I'm going to do it in my plugin:
__________

// Given a post id, get a list of connected posts, either *to*, or
*from*that post
function get_connected( $post_id, $direction ) {
        if ( 'to' == $direction )
            return get_objects_in_term( self::to_term( $post_id ), 'p2p' );
        else
            return wp_get_object_terms( $post_id, 'p2p', 'fields=names' ) )
);
}

// Get all connected posts to a movie
$connected_ids = get_connected( $movie_id, 'to' );

// Next, get only the actors
$posts = get_posts( array( 'post_type' => 'actor', 'post__in' =>
$connected_ids ) );
__________

It's a little bit out of context ( 'p2p' is the name of the glue taxonomy ),
but I hope you get the idea.


-- 
http://scribu.net


More information about the wp-hackers mailing list