Skip Navigation

[Resolved] How to show the related posts through a view (referring to an earlier threat)

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.

This topic contains 4 replies, has 2 voices.

Last updated by matthiasV-2 8 months ago.

Assigned support staff: Waqar.

Author
Posts
#2170169

Refer this ticket to Waqar.

Referring to this ticket: https://toolset.com/forums/topic/introducties-should-only-show-for-connected-experts/ and the original threat.

Waqar,

I'll break down the issue below:

The page hidden link includes a view to show the "eigendommen" posts BUT the page should ONLY SHOW "kopers" the "eigendommen" posts for which they have been "connected" via the relationship "kopers eigendommen".

Here's an example:

Let's say there are the following eigendommen (post type "eigendommen"):

- eigendom A.
- eigendom B.
- eigendom C.

Koper A (post type "kopers") has been connected to eigendom A and B.

Koper A should ONLY SEE eigendommen A and B on the page "hidden link" AND NOT eigendom C.

Each "koper" user will have a single post in the post type "kopers" and he/she will be set as the author of that post.

We've connected the user/koper demo@landman.re to several eigendommen for testing purposes.

What query filter or code snippet should we apply to achieve the above behavior?

#2170697

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

The code snippet for this would be similar to the one suggested here:
https://toolset.com/forums/topic/split-how-to-show-the-related-posts-through-view/#post-1989169

You'll replace all instances of:

- View ID "5663" for the view "Introductie ingelogde gebruikers", with the view ID "84396" for the view "Eigendommen - kaart met filters - acquisitie opportuniteiten professionele kopers".

- The post type slug "vakexpert" with "koper".

- The relationship slug "vakexpert-introductie" with "koper-eigendom".


add_filter( 'wpv_filter_query', 'wpv_filter_query_84396_func', 1000 , 3 );
function wpv_filter_query_84396_func( $query_args, $view_settings ) {
    // skip if blocks edit screen
    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
        return $query_args;
    }
     
    // process if specific view
    if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 84396) ) {
         
        $current_user_id = get_current_user_id();
 
        if($current_user_id > 0) {
            // get current user's "koper" post
            $args = array(
                'post_type'        => 'koper',
                'posts_per_page'   => 1,
                'post_status'      => 'publish',
                'author'           => $current_user_id,
                'fields'           => 'ids',
            );
 
            $posts_array = get_posts( $args );
 
            if(!empty($posts_array)) {
                $query_args['toolset_relationships'] = array( 'role' => 'child', 'related_to' => $posts_array[0], 'relationship' => 'koper-eigendom' );
            }
            else
            {   
                // setting the post type to a non-existent post type so that no results are shown
                $query_args['post_type'] = 'xyz';
            }
        }
        else
        {
            // setting the post type to a non-existent post type so that no results are shown
            $query_args['post_type'] = 'xyz';
        }
    }
    return $query_args;
}

regards,
Waqar

#2170871

Added the above code snippet, however no properties were found: hidden link.

I was logged in as demo@landman.re for which ONLY properties that this user was connected to should show.

#2171023

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

During troubleshooting, I noticed custom code snippet seems to be working correctly, but, the "demo" user ( email: "demo@landman.re" and user ID: "759" ) that you're testing with, has no "Kopers" post, as an author.
hidden link

I had to turn on the "author" option from the "Kopers" post type's settings and you'll now see the author column in the post list.
( screenshot: hidden link )

You'll also see the author field on the edit screen for the single "Kopers" post.
( screenshot: hidden link )

You can set the "demo" user as the author of the most recent "landman" "Kopers" post and you'll see that exactly 21 results will be shown by the view, as 21 eigendommen posts are connected to this "landman" kopers post.

#2171163

My issue is resolved now. Thank you Waqar!