I created a view and it is now showing all posts from [A], with and without a relationship with [B].
I am trying to create a view to show all post from [A] that has a relationship with [B].
Post type [A] and [B] has a one-to-many relationship.
Hi,
Thank you for contacting us and I'd be happy to assist.
Do you need to show the posts from post type [A] which are related to:
- any specific post from a post type [B]
or
- any post from post type [B]?
Also if you could share temporary admin login details, along with the link to the page with this view, I'll be in a better position to see how the view is set up and suggest the next steps, accordingly.
Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.
regards,
Waqar
Thank you for sharing these details.
To filter out the "Agent" posts, without any "Review" posts, you'll need a custom function attached to the filter:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
The custom function will cycle through all the agent posts and look for their related review posts, and for the ones without any related posts, set them for the exclusion by the view's query.
For example:
add_filter( 'wpv_filter_query', 'filter_agents_without_review_fn', 1000 , 3 );
function filter_agents_without_review_fn( $query_args, $view_settings ) {
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 35570) ) {
$post_type = "agent";
$relationship_slug = "agent-review";
$args = array(
'posts_per_page' => -1,
'post_type' => $post_type,
'post_status' => 'publish',
);
$posts_array = get_posts( $args );
foreach ($posts_array as $post_array ) {
$get_results = toolset_get_related_posts( $post_array -> ID, $relationship_slug, 'parent', 1, 0, array(), 'post_id', 'child' );
if(empty($get_results)) {
$query_args['post__not_in'][] = $post_array -> ID;
}
}
}
return $query_args;
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/