Support Forum
Support › MB Relationships › ✅pre_get_posts to get all posts connected to single object › Reply To: pre_get_posts to get all posts connected to single object
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',
),
) );
} );