WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Filtering WP_Query by specific connected item (5 posts)

  1. briandichiara
    Member
    Posted 1 year ago #

    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?

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    'ID' is not a recognized WP_Query query var. Try:

    $args['connected_query'] = array(
      'post__in' => array( $subject )
    );

    Or, even easier:

    $args['connected_items'] = $subject;
  3. briandichiara
    Member
    Posted 1 year ago #

    Ok, so maybe I'm doing something wrong, I tried both of these and it doesn't seem to be filtering my query in the way I expected it. Did my question make sense? I'm trying to show all Documents, then allow the user to filter ALL documents by a specific connected subject, much like you would a taxonomy query, but since I need content for my subjects, I had to use Posts to Posts. Here's what my code looks like:

    $args = array(
    	'post_type' => MY_DOCUMENT_CPT,
    	'posts_per_page' => $limit,
    	'paged' => $current_page
    );
    $subject = isset($_GET['subject']) && !empty($_GET['subject']) ? sanitize_text_field($_GET['subject']) : NULL;
    if($subject){
    	$args['connected_type'] = MY_P2P_CONNECTION;
    	$args['connected_to'] = MY_SUBJECT_CPT;
    	$args['connected_query'] = array(
    		'post__in' => array($subject)
    	);
    }
    $documents = new WP_Query($args);
  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    Oh, the problem is that you're using both 'connected_to' and 'connected_query'.

    Replace 'connected_to' with 'connected_direction' => 'to'.

    Also, don't define the 'post_type' when filtering.

  5. briandichiara
    Member
    Posted 1 year ago #

    Yay! That worked. Thank you!!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic