Skip Navigation

[Escalated to 2nd Tier] Sorting by Last Modified not being respected on Front End

This support ticket is created 3 years, 9 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 19 replies, has 2 voices.

Last updated by Minesh 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1704999

Hi Support

We would like to sort our Job Ads and Nanny Ads by Last Modified instead of Post Date. I saw several posts on the forum regarding the topic and tried a few things but I still can't get the Views to display by Last Modified instead of Post Date.

1) In the Views "Find a Native Nanny Search and Results View" and "Find a Job Search and Results View", I have set "Order by" to Last Modified Descending but both Views are still ordering by Post Date Descending.

2) What would be the correct syntax for the ORDER BY [wpv-sort-orderby] criteria for Last Modified date? As an example in the Loop Editor of the View "Find a Native Nanny Search and Results View", I tried to use the syntax post_date type='modified' instead of just post_date

3) I wasn't able to find any documentation for [wpv-sort-orderby] under https://toolset.com/documentation/programmer-reference/views/views-shortcodes/

Thanks and regards
Simon

#1705057

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please check now: hidden link

I've adjusted the shortcode as given under and for modified date, you need to use the work modifield.

 [wpv-sort-orderby 
      type="select" 
      options="modified,field-wpcf-nanny-years-experience,field-wpcf-hourly-rate-euro" 
      label_for_modified="Date Modified" 
      label_for_field-wpcf-nanny-years-experience="Years Experience" 
      label_for_field-wpcf-hourly-rate-euro="Hourly Rate (€)" 
      orderby_as_numeric_for="field-wpcf-nanny-years-experience,field-wpcf-nanny-hourly-rate-euro" 
      orderby_ascending_for="field-wpcf-hourly-rate-euro"
      orderby_descending_for="modified,field-wpcf-nanny-years-experience"]&

You should make changes to your other views where required.

#1705237
Screenshot 2020-07-13 at 15.16.54.png

HI Minesh

Thanks for the update. It seems to be sorting correctly by Last Modified if I actively choose it from list of options, but the list of options still contains Post Date when you first call up the page hidden link , although it is not listed in the orderby select (see screenshot).

When I change the SORT BY to Last Modified, the list refreshes correctly, then "Post Date" is no longer selectable from the list (as required), but why is Post Date still an option there when you go open the page Find a Native Nanny? I tried clearing my browser cache in case it was that, but it is still there after clearing that too.

Thanks and regards
Simon

#1705271

Minesh
Supporter

Languages: English (English )

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

The sorting dropdown adds "Post Date" option by default even though you deleted it - this is known issue to us and already reported to our devs.

For now, as a workaround, I've added the following code to your view's "Search and Pagination" section:
=> hidden link

jQuery(document).ready(function($){
  
  $('select[name="wpv_sort_orderby"] option[value="post_date"]').remove();

});
 

I can see now post date option is not available now.

#1705277

Hi Minesh

I see the option removed, however the sorting is still defaulting to Post Date Descending in the background. When a user first goes to Find a Native Nanny, the list is is still being shown by Post Date Descending, although the SORT BY list is showing Last Modified Descending. In order to get the list to be truly Last Modified Descending, the user would have to actively choose another sorting rule, (eg change to Ascending), and then choose Last Modified Descending to get the list as required.

In other words, the Last Modified Descending is not the default sort order, as required, and is currently visible in the SORT BY.

Kind regards
Simon

#1705607

Minesh
Supporter

Languages: English (English )

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

As a workaround to set default post orderby to modified date, I've added the following view's filter hook to "Custom Code" section of Toolset with code snippet namely "set-orderby-modified"
=> hidden link

function func_set_default_orderby_modified( $view_args, $view_settings, $view_id ) {
 
  if($view_id == 2145 and !wp_doing_ajax()) {
    $view_args['orderby'] = 'post_modified';
   
 }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_set_default_orderby_modified', 101, 3 );

Can you please confirm that now its working as expected: hidden link

#1707649

HI Minesh

Can you please contact your devs and ask for an approximate indication of when the bug will be fixed? Weeks/months? Has it reached QA stage yet?

I would like to avoid Custom Code where at all possible, however I understand it is unavoidable in some cases.

Regarding your last piece of code (in case the bug is not fixed for a while):
1) What are the numbers 101, 3 at the end of your code for?
2) If we are not using AJAX, can we just drop this in the code? and !wp_doing_ajax()
3) What would be the syntax to add other views into this code, so we don't have to write a new piece of code for every view which has the bug?

Kind regards
Simon

#1707661

Minesh
Supporter

Languages: English (English )

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

There is no ETA on when the fix is provided as the issue is just reported and our Devs take care of it as its totally depends on their pre-defined priority and work.

Regarding your last piece of code (in case the bug is not fixed for a while):
1) What are the numbers 101, 3 at the end of your code for?
==>
The 101 is the hook priority and 3 is the number of argument you will pass to the function.
More info:
- https://developer.wordpress.org/reference/functions/add_filter/

2) If we are not using AJAX, can we just drop this in the code? and !wp_doing_ajax()
===>
If you drop ajaxt, the code needs to be again adjusted in the case of not using AJAX.

3) What would be the syntax to add other views into this code, so we don't have to write a new piece of code for every view which has the bug?
===>
To apply multiple view IDs the code should be adjusted as given under:

function func_set_default_orderby_modified( $view_args, $view_settings, $view_id ) {
  $target_view_ids = array(2145,999,222);
  if( in_array($view_id, $target_view_ids ) and !wp_doing_ajax()) {
    $view_args['orderby'] = 'post_modified';
    
 }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_set_default_orderby_modified', 101, 3 );

Where:
- You can adjust your multiple target view IDs with $target_view_ids array. Where 999 and 222 I've added as dummy view IDs for the example purpose.

#1712657

HI Minesh

I need a little more time to test this fully before it can be closed.

Thanks and regards
Simon

#1714421

Minesh
Supporter

Languages: English (English )

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

Sure - Please feel free to take your time.

#1715187

Hi Minesh

OK, I have modified the code for the 2 views. It appears to be working as we need it, but could I ask you to just have a quick look at the code to verify it? I just added the other view to the array and removed the AJAX part.

If the code is OK, our issue is temporarily resolved with a workaround. I would prefer to keep the ticket open until the underlying bug is resolved. Please set it to "Escalated to 2nd tier" like our other open bugs and send us a quick update when the underlying bug with Post Date has been fixed.

Thank you and kind regards
Simon

#1715551

Minesh
Supporter

Languages: English (English )

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

Ok - I will update you when I will get any updates on it.

#1715763

Hi Minesh

Did you check to see if my code changes were correct?

Thanks and regards
Simon

#1715781

Minesh
Supporter

Languages: English (English )

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

Why you removed the ajax part - you should keep your view to filter using ajax and use the same code I've shared with the following reply where you just need to adjust your view IDs within the code I shared.
=> https://toolset.com/forums/topic/sorting-by-last-modified-not-being-respected-on-front-end/#post-1707661

As you drop AJAX part, the sorting is not triggeted. Please set your view to use the AJAX and as before and use the code I shared with above reply (for which I shared the link) and just replace view IDs within the code.

#1715785

Hi Minesh

We have to live without AJAX for the moment. I dropped the AJAX part because when we use AJAX the search results are not inconsistent due to translation issues with WPML. We have another ticket open about that.

Kind regards
Simon