Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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 5 years ago by jan_behr.
    #1165068

    Hey 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_sort

    Best regards,
    Ismael

    #1165149

    Hey 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
    Jan

    #1165425

    Hi,

    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,
    Ismael

    #1165451

    Hi,

    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
    Jan

    #1166327

    Hi,

    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,
    Ismael

    #1167044

    Hi 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
    Jan

    #1168040

    Hi,

    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

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.