Tagged: sorting, woocommerce
-
AuthorPosts
-
December 11, 2019 at 11:25 am #1164778
Dear Enfold Support,
on Product-Pages (Single Product) there is at tle left and the right side of the scrren (of course only on Desktops) the possibility to switch to the next/previous product (next/prev post).
https://www.bilder-upload.eu/bild-4296e7-1576054468.png.html
I want to manage the order of the product when you follow previous/next. I want to have here the same order as the “default order” in WooCommerce, but I only get the order of the products in the backend (which is “order by date / ascending”).
The advantage of the “default order” is, that you can sort your poducts in the way you want within WooCommerce. The “order by date” leads to a somewhat chaotic order, when you add products from time to time. In my view the next/prev Poduct should be well selected and related product.
You can see this issue directly in the enfold demo: https://kriesi.at/themes/enfold-2017/shop/
Is there any way to get the “default order” on the next/prev Selectors?
Thanks and best regards
Jan- This topic was modified 4 years, 11 months ago by jan_behr.
December 12, 2019 at 6:07 am #1165068Hey Jan,
Thank you for the inquiry.
You can alter the default sorting of the items in the post navigation by using the following filters.
function avf_get_prev_post_sort_mod( $sort ) { return "ORDER BY p.post_title DESC LIMIT 1"; } add_filter( 'get_previous_post_sort', 'avf_get_prev_post_sort_mod' ); function avf_get_next_post_sort_mod( $sort ) { return "ORDER BY p.post_title ASC LIMIT 1"; } add_filter( 'get_next_post_sort', 'avf_get_next_post_sort_mod' );
This will sort the items by title instead of the date they were published. Check the documentation below for more info.
// https://adambrown.info/p/wp_hooks/hook/get_previous_post_sort
// https://adambrown.info/p/wp_hooks/hook/get_next_post_sortBest regards,
IsmaelDecember 12, 2019 at 10:50 am #1165149Hey Ismael,
thanks for your reply! You directed me to the right searcvh term the filter “get_next_post_sort”.
I found this Blog post solving this general issue which is present in the prev/next post navigation on all custom post types (woocommerce products are custom post types):
The Code is:
function kb_custom_adjacent_post_where($sql) { if ( !is_main_query() || !is_singular() ) return $sql; $the_post = get_post( get_the_ID() ); $patterns = array(); $patterns[] = '/post_date/'; $patterns[] = '/\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\'/'; $replacements = array(); $replacements[] = 'menu_order'; $replacements[] = $the_post->menu_order; return preg_replace( $patterns, $replacements, $sql ); } add_filter( 'get_next_post_where', 'kb_custom_adjacent_post_where' ); add_filter( 'get_previous_post_where', 'kb_custom_adjacent_post_where' ); function kb_custom_adjacent_post_sort($sql) { if ( !is_main_query() || !is_singular() ) return $sql; $pattern = '/post_date/'; $replacement = 'menu_order'; return preg_replace( $pattern, $replacement, $sql ); } add_filter( 'get_next_post_sort', 'kb_custom_adjacent_post_sort' ); add_filter( 'get_previous_post_sort', 'kb_custom_adjacent_post_sort' );
It woud be great, if the code snippet could find its way into one of the next releases of Enfold. I think this is of general interest and will make Enfold one step more hassle-free.
Thanks and best regards
JanDecember 13, 2019 at 8:50 am #1165425Hi,
Thanks for sharing.
The navigation is also used for posts, portfolio and other post types, so the default sort is probably more appropriate. You can leave the above filter as is for your products.
Best regards,
IsmaelDecember 13, 2019 at 10:26 am #1165451Hi,
you are correct, that the navigation is used also for posts etc. and is working fine there.
But it is also used for products and there the prev/next navigation is broken (the navigation does not use the default WooCommerce order).
So it would be good, if the Enfold prev/next navigation works in one of the next releases in all circumstances, products included.
Kind regards
JanDecember 16, 2019 at 12:22 pm #1166327Hi,
The above modification actually sorts the products by menu order. The product post type by default doesn’t support that feature, so it has to be enabled manually. Did you turn on the Page Attributes > Menu Order field for your products?
// https://developer.wordpress.org/reference/functions/register_post_type/
Best regards,
IsmaelDecember 18, 2019 at 12:00 am #1167044Hi Ismael,
I‘m not shure what you mean with
you turn on the Page Attributes > Menu Order field
I set on all products the Menu_Order Field to achieve a nice order of the products. This works fine for the default shop and product grids, because menu_order (+ Name) is the default sorting for products in the Shop and grids.
But I learned, that posts have date order as default and do not support menu_order and that leads to a different behaviour in the shop and prev/next navigation.
My „Problem“ is solved with this code fragment. I just wondered, if it makes sense to use the default product sorting menu_order in general for the prev/next navigation for products.
Kind regards
JanDecember 20, 2019 at 5:52 am #1168040Hi,
Thank you for the info.
What do you mean exactly when you say Menu_Order Field? Is that the Order field in the Page Attributes meta box where you can sort posts manually based on the integer value? Please check the documentation below for reference.
// https://en.support.wordpress.com/pages/page-options/
If that is not what you’re referring to, then the filter above is not really doing what it’s supposed to because what it does is change the sort order based on the value of the Order field.
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.