Saltar navegación

[Resuelto] Filter view with only posts having a relationship

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

Trying to filter a view with only posts having a relationship defined, that is a post connected.

Solution:

It needs custom codes, for example:

https://toolset.com/forums/topic/filter-view-with-only-posts-having-a-relationship/#post-2288425

Relevant Documentation:

This support ticket is created hace 2 años, 11 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Este tema contiene 9 respuestas, tiene 2 mensajes.

Última actualización por poM hace 2 años, 11 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#2286977

poM

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?

enlace oculto

#2286981

poM

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.

#2286987

poM

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;

}

#2287441

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

#2287517

poM

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.

#2287565

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

#2287625

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

#2287641

poM

The view is displayed here : enlace oculto

And can be administered here : enlace oculto

Cheers

#2288425

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

#2288505

poM

It perferctly works, thanks a lot for your assistance !

I have copied the code in my child theme.

Cheers