Skip Navigation

[Resolved] Relationships and views with query filter and custom sorting

This support ticket is created 4 years, 4 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by nicholasE 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1727489

Hi
I am creating a website with one to many relationship between a post type called Parks (one) and a post type called Park Homes (many). The relationship I have set up for this is called "Park to Park Homes"

I have created a content type to display a single Park (Parks post type). This page has a link off to another page that will show all Park Homes (Park Home post type) related to that Park.

The Park page link is created in javascript to include a query string parameter that uses the current Park "post id". So, the link to the Park Homes page looks something like: /park-homes/?wpv-relationship-filter=211 where 211 is the post id for the current "Park"

The Park Homes page has a view of Park Homes. This is filtered using a query filter: Select posts in a Park to Park Homes relationship that are related to the Post with ID set by the URL parameter wpv-relationship-filter.

Basically this is working but I have two problems.

1. If you navigate to the /park-homes/ page without applying the wpv-relationship-filter parameter that page displays all Park Home posts. Shouldn't it display no posts if the query string parameter is null?
2. More importantly, I am trying to add sorting options to the /park-homes/ page. I have added sorting options and set the sort to apply without re-loading the page. The idea being that once on the page, the user could apply a custom sort within the displayed Park Homes. However, applying a new sort (even with sort set to not reload page) clears the query filter (and removes the query string parameter from the url).

Is there a way around this or a better way to achieve this functionality?

i.e. I want to go from a page displaying a "Park" to a page displaying all "Park Homes" belonging to that "Park". Once on the "Park Homes" page I want to be able to apply a custom sorting without losing the view query filter.

Thanks in advance.

Nick

#1727971

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

1. If you navigate to the /park-homes/ page without applying the wpv-relationship-filter parameter that page displays all Park Home posts. Shouldn't it display no posts if the query string parameter is null?
==>
By default, view displays all results before you apply the filter that means when the view is loaded first. If you do not want to display any results until the filter is selected we have workaround. If you can share problem URL and access details I am happy to look at it.

2. More importantly, I am trying to add sorting options to the /park-homes/ page. I have added sorting options and set the sort to apply without re-loading the page. The idea being that once on the page, the user could apply a custom sort within the displayed Park Homes. However, applying a new sort (even with sort set to not reload page) clears the query filter (and removes the query string parameter from the url).
==>
I will require to review this issue if you can share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.\

I have set the next reply to private which means only you and I have access to it.

#1728035

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I can see now no records are displayed on /homes-for-sale/ page.

I've added the following view's filter hook to the "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_filter_query', 'func_hide_park_by_default', 99, 3 );
function func_hide_park_by_default( $query_args, $view_settings, $views_id  ) {
    if ( $views_id == 366 && !isset($_GET['wpv-relationship-filter']) ) {  
        $query_args['post__in']= array(0);
    }
    return $query_args;
}

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Can you please confirm it works at your end as well.

#1728075

Hi Manesh

Yes that is working. Thank you. However, the sorting on the /homes-for-sale/ page is not working correctly.

If you go to /homes-for-sale/?wpv-relationship-filter=211 first of all the page displays ok. If you modify the sort by selection twice, the "?wpv-relationship-filter=211" parameter is removed from the url and the page now shows no homes.

As I said, the sort is configured to not refresh the entire page.

Thanks

Nick

#1728105

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

It is not worth to sort with only one entry as it does not make any sense.

May I kindly ask you to add few records to your page /homes-for-sale/?wpv-relationship-filter=211 , maybe at least 5 records so I can check whats the issue.

#1728167

I've added some more entries now.

Navigate to /homes-for-sale/?wpv-relationship-filter=211 and you will see 4 items.

Click on the sort field and choose "Field Price". The items are sorted correctly using the default sort order "Lowest First".

Now click on the sort order to change it to "Highest First". All entries now disappear.

Thanks

#1728253

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I just tried to reproduce the issue with my local test install and with my test site the filtering and sorting works just fine and when I change the sorting option the URL is updated with URL param as expected.

In order to minimise the cause cause of the issue and possible conflict with theme or other third party plugins:
Could you please try to resolve your issue by deactivating all third-party plugins as well as the default theme to check for any possible conflicts with any of the plugins or themes?
- Do you see any difference?

What if you try to create a brand new view to display your related posts and check if that helps?

#1728399

Hi Minesh

I have deactivated third party plugins and switched over to a default wordpress theme. I still see the problems.

For the sorting control, there are two options for how the sorting is applied:

Reload the page
Without reloading the page.

I had selected "Without reloading the page". In this case, I see the operation I described before, i.e.

Navigate to "/homes-for-sale/?wpv-relationship-filter=211". The page displays correctly with default sorting and shows 4 post items.

Change sort field to be "Field - Price". The page updates with correct sorting. However, at this point the query string parameter "wpv-relationship-filter=211" is already removed from the url.

Change sort direction. The page now updates and shows no items.

I also tried with the Apply sorting option changed to "Reload the page". In this case, I see the following:

Navigate to "/homes-for-sale/?wpv-relationship-filter=211". The page displays correctly with default sorting and shows 4 post items.

Change sort field to be "Field-Price". The page now reloads but shows no items. If I look at the url it is now showing:

"/homes-for-sale/?wpv_view_count=366&wpv_sort_orderby=field-wpcf-park-home-price&wpv_sort_order=asc&wpv_sort_orderby_as=string"

The original query string parameter of wpv-relationship-filter=211 has been removed.

So, in both cases, "Reload the page" or "Without reloading the page" the sorting control is not respecting the original query string parameter of wpv-relationship-filter=211 when the page updates. This parameter is essentially being removed.

Thanks

#1728447

Hi Minesh

I think I have fixed the issue. The solution seems to be to go to Toolset>Settings>Front-end Content and click to "enable history management settings for AJAX custom search".

Then if I choose to Apply sorting "without reloading the page", the page seems to work ok.

Thanks for your help.