Custom Fields
Re-Envisioned
Support MB Relationships pre_get_posts to get all posts connected to single object

This topic contains 16 replies, has 2 voices, and was last updated by  Clayton Collie 7 months, 3 weeks ago.

  • Creator
    Topic
  • #11651
    Resolved
    Clayton Collie
    Participant

    I have a listing of people that I am showing on the post type archive. For taxonomies I can use a simple query var with the taxonomy term to filter out the listing of all posts into the posts only inside that taxonomy. I would like to do the same thing with relationships.

    Is there a built-in query var for relationships? I think that is relationship. But then how would I use it in the URL bar? Pass a single ID?

    for example – domain.com/people/?relationship=320

    What I was thinking about doing is add a custom query arg then pass a single post ID then use that post ID inside a pre_get_post filter to only show the posts related to that ID. Does that sound like a good plan?

    
    function custom_query_vars_filter($vars) {
        $vars[] .= 'relationships';
        return $vars;
    }
    add_filter( 'query_vars', 'custom_query_vars_filter' );
    
    
    function prefix_get_related_posts( $query ) {
        
        $post_id = get_query_var('relationships');
        
        if ( empty( $post_id ) || is_admin() ) {
            return $query;
        }
    
        if( is_post_type_archive( 'poa_person' ) && $query->is_main_query() ) {
            $relationship = array( 
                'id' => 'poa_practice_to_poa_person',
                'from' => $post_id,
            );
            $query->set( 'relationship', $relationship );
            }
    
        return $query;
    }
    add_filter( 'pre_get_posts', 'prefix_get_related_posts', 10, 1);
    

    This does not work just yet. Can you help shed some light on this query?

Viewing 10 replies - 1 through 10 (of 16 total)
  • Author
    Replies
  • #11659

    Anh Tran
    Keymaster

    Hi Juanita,

    You’re right about the query var. And your idea of adding the query var is great. I think the only problem is using pre_get_posts is too late. The plugin uses parse_query hook to setup some parameter for the SQL. So you might want to change your code from pre_get_posts to parse_query.

    #11663

    Clayton Collie
    Participant
    
    function custom_query_vars_filter($vars) {
        $vars[] .= 'relationships';
        return $vars;
    }
    add_filter( 'query_vars', 'custom_query_vars_filter' );
    
    function prefix_get_related_posts( $query ) {
        
        $post_id = get_query_var('relationships');
        
        if ( empty( $post_id ) || is_admin() ) {
            return $query;
        }
    
        if( is_post_type_archive( 'poa_person' ) && $query->is_main_query() ) {
            $query->set( 'relationship', 
                array( 
                    'id' => 'poa_practice_to_poa_person',
                    'from' => $post_id,
                )
            );
        }
    
        return $query;
    }
    add_filter( 'parse_query', 'prefix_get_related_posts', 10, 1);
    

    parse_query works great. But now my styling is all jacked up. At least the query is working now. Thanks for the help.

    #11664

    Clayton Collie
    Participant

    On second look, parse_query is throwing off the entire query so the post type archive doesn’t even get the post type it is associated with. I am getting the proper related post just fine but I am loosing the post type for the entire query. It seems like I am overriding my entire query and just setting the relationship query instead of adding the two queries together.

    #11683

    Anh Tran
    Keymaster

    Hi Juanita,

    The parse_query hook runs just before pre_get_posts and there’s nothing between them. See this screenshot:

    View post on imgur.com

    So, if your code works with pre_get_posts, it should work with parse_query. I guess the problem might come from other things.

    #11694

    Clayton Collie
    Participant

    I disabled every plugin and reverted to default WP theme to test. I am still stumped. Have a look at the two screenshots. First one is the base query where the query and query_vars match. This query assigns the post type to every other function to get things like post type archive title, etc.
    basic query

    Second is when the query_vars are used to alter the query. Notice how the query and query vars are different. The filtering of posts works great to show only the post that is related to 2735.
    altered query

    Can you add this code to your install and let me know if you are seeing the same errors?

    
    add_action( 'parse_query', 'poa_filter_query_by_relationship', 10, 1 );
    /**
     * Filter the main query by using URL parameters
     * 
     * @param object $query WP_Query.
     * @return object
     * 
     * @since 3.6.8
     */
    
    function poa_filter_query_by_relationship( $query ) {
        
        $id     = get_query_var('relation_id');
        $from   = get_query_var('relation_from');
        $to     = get_query_var('relation_to');
        
        if ( ! empty( $id ) && ! is_admin() && $query->is_main_query() ) {
    
            if ( ! empty( $from ) ) {
                
                $query->set( 'relationship', 
                    array( 
                        'id'  => esc_attr( $id ),
                        'from'    => intval( $from ),
                    )
                );
    
                var_dump($query);
    
            } elseif ( empty( $to ) ) {
    
                $query->set( 'relationship', 
                    array( 
                        'id'  => esc_attr( $id ),
                        'to'  => intval( $to )
                    )
                );
            }
            
        }
    
    }
    
    
    add_filter( 'query_vars', 'add_query_vars' );
    /**
         * Add query vars to build query with URL parameters
         *
         * @param array $vars WP_Query variables.
         * @return array
         * @since 3.6.8
         */
        public function add_query_vars( array $vars ) {
            $vars[] = 'relation_id';
            $vars[] = 'relation_from';
            $vars[] = 'relation_to';
            return $vars;
        }
    
    #11702

    Anh Tran
    Keymaster

    Hi Juanita,

    I can’t see 2 screenshots :(.

    But I think the problem is the way you get query var. The get_query_var should not be used in this situation as it gets the value from the main query. Please replace the code with:

    $id = $query->get('relation_id');
    $from = $query->get('relation_from');
    $to = $query->get('relation_to');
    #11715

    Clayton Collie
    Participant

    Still not working even thought changed how I am getting the query_var.

    Did you try running that code a post type archive? That is what I am doing and seeing part of the query being altered. Its weird. It shows the proper post type objects but doesnt have the proper values for sending the post type to things like post_type_archive_title.

    altered

    base

    #11716

    Clayton Collie
    Participant

    base

    altered

    #11717

    Clayton Collie
    Participant
    #11746

    Anh Tran
    Keymaster

    Hi Juanita,

    Sorry to get back late. I tried your code and created a video on that: http://recordit.co/BlAq5rhUVr. The query seems fine to me. Please take a look.

Viewing 10 replies - 1 through 10 (of 16 total)

You must be logged in to reply to this topic.