Skip Navigation

[Closed] Order posts based on relationship

This support ticket is created 2 years, 11 months ago. 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.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 2 years, 11 months ago.

Author
Posts
#2276093

This is a followup question for this topic: https://toolset.com/forums/topic/how-to-display-dynamic-content-on-pages-based-on-a-selection-on-the-landing-page/

I created a relation between my custom posttype "kappers" and WordPress posts. I also created a view to display those post.

Now, I want to show the connected posts for the current "kapper" (based on an ID in the URL) first, and the rest of the posts after that.

#2276165

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You pass the ID of a kapper post in the URL.

On that page you want to first display standard Posts that are related to that kapper.

Then you want to display Posts that are not related to that kapper, yes?

You would need two separate Views for that. The first View includes a Query Filter to show posts related to the kapper set by the URL parameter.

The second needs to be the opposite, but Views doesn't let you set up a negative filter like that.

You would need to use the API filter wpv_filter_query to modify the query arguments programmatically.

With a relationship filter like this Views first queries the related posts that could appear in the results, and then specifies those in the post__in argument of the resulting WP query.

To only show posts that are not related, you could switch that argument to become a post__not_in argument.

Here's an example of the code you could use to do that for the second View:

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    if (in_array($view_id, array( 123 )) && isset( $view_args['post__in'] )) {

        $view_args['post__not_in'] = $view_args['post__in'];
        unset( $view_args['post__in'] );
        
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

You would need to edit the View ID (123 in the sample above, and you can add multiple View IDs to the array if you want to apply the same to more than one View).

The topic ‘[Closed] Order posts based on relationship’ is closed to new replies.