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  Content Pilot 9 months, 3 weeks ago.

  • Creator
    Topic
  • #11651
    Resolved
    Content Pilot
    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 6 replies - 11 through 16 (of 16 total)
  • Author
    Replies
  • #11762

    Content Pilot
    Participant

    Awesome video. Thank you so much for going into so much detail. This is exactly what I am after.

    I could have sworn that I saw errors on the default theme. I might have some rogue pre get posts that I need to sort out in my main plugin that is also housing the relationship fields. I will do some more digging and report back.

    #11765

    Content Pilot
    Participant

    I am following the video EXACTLY and am still seeing errors. OMG this is so frustrating. Your video is perfect and is not throwing errors. I setup the freshest install and just installed metabox and mb relationships with custom post types built all in the same file. Made the relationships and ran the query on the default theme like your video. Still seeing the post type being stripped for certain functions like get_post_type_object. I actually think get_post_type_object is returning null since the post type is being sent as a string not an array or object. Is_scalar is exiting early for get_post_type_object.

    See the screenshots. I am getting the exact same query you producing with post type being sent as a string.
    query and erorrs

    Running WP 4.9.8 on Flywheel
    MySQL 5.6.34
    PHP 7.2 and 7.0.3
    Nginx
    Twenty Seventeen theme

    Could that have anything to do with it?

    #11766

    Content Pilot
    Participant

    d

    gg

    s

    #11767

    Content Pilot
    Participant

    For some reason my query is setting post type to any. For some reason in parse_query it will change the post type any when using a tax_query. I don’t have a tax_query nor do I have any taxonomies on these two post types.

    https://developer.wordpress.org/reference/classes/wp_query/parse_query/

    Search for post_type

    add_action( 'parse_query', 'rel_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 rel_filter_query_by_relationship( $query ) {
        
        $id     = $query->get('relation_id');
        $from   = $query->get('relation_from');
        $to     = $query->get('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 ),
                    )
                );
    
            } elseif ( ! empty( $to ) ) {
    
                $query->set( 'relationship', 
                    array( 
                        'id'  => esc_attr( $id ),
                        'to'  => intval( $to )
                    )
                );
            }
            
            
        }
    
    }
    
    add_filter( 'query_vars', 'rel_add_query_vars' );
    /**
     * Add query vars to build query with URL parameters
     *
     * @param array $vars WP_Query variables.
     * @return array
     * @since 3.6.8
     */
    function rel_add_query_vars( array $vars ) {
        $vars[] = 'relation_id';
        $vars[] = 'relation_from';
        $vars[] = 'relation_to';
        return $vars;
    }
    
    add_action( 'mb_relationships_init', function() {
        MB_Relationships_API::register( array(
            'id'   => 'practices_to_persons',
            'from' => array(
                'object_type' => 'post',
                'post_type' => 'practice',
                'admin_column' => 'after title'
            ),
            'to'   => array(
                'object_type' => 'post',
                'post_type' => 'person',
                'admin_column' => 'after title',
            ),
        ) );
    } );
    #11769

    Anh Tran
    Keymaster

    Oh, I know why. I’m using the development version, which fixes the post type set to any. I’ve just updated the plugin on wordpress.org. Please update and try again!

    Sorry for the confusion.

    #11777

    Content Pilot
    Participant

    Ahhhh. Thank you for updating the plugin. I am off to the races now! Thanks for sticking with me on this one.

Viewing 6 replies - 11 through 16 (of 16 total)

You must be logged in to reply to this topic.