Filtering WP_Query by specific connected item
-
I’m trying to figure out how to adjust WP_Query whenever a user has chosen to filter the results based on connected post. So, for example, for a document database, I have the post type “document” and the post type “subject”. If the user chooses “Math”, i need to filter all “document” post types by “Math” (rather the ID of “Math” would be better). Here’s what I tried, but it’s not working:
if($subject){ $args['connected_type'] = 'document_to_subject'; $args['connected_to'] = 'subject'; $args['connected_query'] = array( 'ID' => $subject ); }
My query end up looking like this:
SELECT SQL_CALC_FOUND_ROWS lhdb_posts.*, lhdb_p2p.* FROM lhdb_posts INNER JOIN lhdb_p2p WHERE 1=1 AND lhdb_posts.post_type = 'document' AND (lhdb_posts.post_status = 'publish' OR lhdb_posts.post_status = 'private') AND (lhdb_p2p.p2p_type = 'document_to_subject' AND lhdb_posts.ID = lhdb_p2p.p2p_from AND lhdb_p2p.p2p_to IN (SELECT lhdb_posts.ID FROM lhdb_posts WHERE 1=1 AND lhdb_posts.ID IN (0) AND lhdb_posts.post_type IN ('subject') AND (lhdb_posts.post_status = 'publish' OR lhdb_posts.post_author = 1 AND lhdb_posts.post_status = 'private') ORDER BY lhdb_posts.post_date DESC )) ORDER BY lhdb_posts.post_date DESC LIMIT 0, 25
It looks close, but the part about “AND lhdb_p2p.p2p_to IN” never actually inserts my ID that i’m passing as the $subject. Am I doing something wrong? Please help!
Also, going one step further, I also need to be able to filter by a taxonomy of the connected posts. Is that possible?
- The topic ‘Filtering WP_Query by specific connected item’ is closed to new replies.