Tell us what you are trying to do?
Trying to filter a view with only posts having a relationship defined, that is a post connected.
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
hidden link
I actually have an event post type with a many to many relationship "eventer-association" connecting them to association post type.
My view is listing events and I would like to use a wpv_filter_query filter to show only those connected to an association post.
Cheers.
I have tried something like the following with no success :
add_filter( 'wpv_filter_query', 'divi_force_shortcode_over_param', 10, 3 );
function divi_force_shortcode_over_param($query, $setting, $views_id) {
// Evts associations
if ($views_id == 53185) {
$posts_in = array();
foreach ( $query->posts as $post ) {
$evt_id = $post->ID;
$assos = toolset_get_related_posts(
$evt_id,
'eventer-association',
array(
'need_found_rows' => true,
'query_by_role' => 'parent',
'limit' => 999,
'return' => 'post_id',
'role_to_return' => 'child'
)
);
if ( !empty($assos) ) {
array_push($posts_in, $post );
}
}
$query['post__in'] = $posts_in;
$query['post__not_in'] = null;
}
Hello,
There isn't such kind of built-in feature, you can consider custom codes, see the solution of similar thread:
https://toolset.com/forums/topic/display-posts-with-a-relationship/#post-1158618
Hi,
Thanks a lot for your reply.
I have already tried this custom code but the toolset_associations does not contain the connected the correct post IDs anymore.
parent_id and child_id does ot fit the IDs of the connected posts.
Since it is a custom codes problem, please provide a test site with the same problem, fill below private message box with login details, I need a live website to test and debug
Thanks for the details, I can login into your development website, please point out the problem page URL and view URL, where I can edit your custom PHP codes, thanks
The view is displayed here : hidden link
And can be administered here : hidden link
Cheers
Thanks for the details, I have done below modifications in your website:
Dashboard-> Toolset-> Settings-> Custom codes, add one item "display-posts-with-a-relationship", with below codes:
function no_parent_query_func1($query_args, $view_settings, $view_id){
if($view_id == 53185){
add_filter( 'posts_where', 'no_parent_posts_where_func1', 10, 2 );
}
return $query_args;
}
function no_parent_posts_where_func1($where){
global $wpdb;
$relationship_slug = 'eventer-association';
$where .= ' AND ' . $wpdb->posts . '.ID IN(
SELECT element_id FROM ' . $wpdb->prefix . 'toolset_connected_elements)';
return $where;
}
function remove_no_parent_posts_where_func1($query_args, $view_settings, $view_id){
if($view_id == 53185){
remove_filter( 'posts_where', 'no_parent_posts_where_func');
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'no_parent_query_func1', 99, 3 );
add_filter( 'wpv_filter_query_post_process', 'remove_no_parent_posts_where_func1', 10, 3 );
Please test again, check if it is fixed, thanks
It perferctly works, thanks a lot for your assistance !
I have copied the code in my child theme.
Cheers