Skip Navigation

[Resolved] Set specific view to order by date, insted of order by price

This thread is resolved. Here is a description of the problem and solution.

Problem:
Set specific view to order by date, insted of order by price

Solution:
You can use the view's ordering section to order by post date DESC

You can find the proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/set-specific-view-to-order-by-date-insted-of-order-by-price/#post-1220811

Relevant Documentation:

This support ticket is created 5 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 6 replies, has 3 voices.

Last updated by ivanJ 5 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1217324

Hello,

I have this custom code for Toolset in which I make all views and product archives ordered by price (from min to max).

<?php
/**
* New custom code snippet.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

function sort_by_regular_price($query) {
if ( $query->is_post_type_archive('product') or $query->is_tax('product_cat') ) {
$query->set('order', 'ASC');
$query->set('meta_key', '_regular_price');
$query->set('orderby', 'meta_value_num');
}
}
add_action('pre_get_posts', 'sort_by_regular_price');

I need one view by id: 6217 to be set order by post date descending (from newset to oldest). Is there a chance you can do it by creating some kind of IF statement in that code section above?

Best regards,
Ivan

#1217435

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - As I can see the code is added to have the effect of orderby on product and product_cat archive.

On what archive you want to change the sorting order? product or product_cat ?

#1218418
view-setup.png
screenshot.png

Hello,

It's this view on the left side under the title "Akcija", that view is separate view (not an archive) and order by post date is not working properly.

I've attached few screenshots.

Best regards,
Ivan

#1218831

Hello,

Yes, it is possible to do some condition check in pre_get_posts action hook, see the example codes in WordPress document:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Examples

For example:

 if ( $query->is_home() && $query->is_main_query() ) {
...
}

This will take effect only on home page's main query

#1219401

Minesh
Supporter

Languages: English (English )

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

Do you need further assistance here? If yes: please share access details so I can review your setup and guide you in the right direction.

*** 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.

#1220811

Minesh
Supporter

Languages: English (English )

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

Can you please check now:
=> hidden link

I've set your view to order by post date DESC withing your view's ordering section:
=> hidden link

As well as - I've adjusted the code little bit as given under:

function sort_by_regular_price($query) { 
    if ( ($query->is_post_type_archive('product') or  $query->is_tax('product_cat')) ) {
      if(!is_front_page()) {
    	$query->set('order', 'ASC');
    	$query->set('meta_key', '_regular_price');
    	$query->set('orderby', 'meta_value_num');
      }
    }
}
add_action('pre_get_posts', 'sort_by_regular_price');
#1220970

My issue is resolved now. Thank you!