• Resolved jmarvid

    (@jmarvid)


    Sort search results:
    Would it be possible to sort the search results by product creation date?
    I would like the most recent to be displayed at the top of the search results page.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @jmarvid

    Go to WordPress Admin Dashboard -> Appearance -> Customize -> WooCommerce -> Product Catalogue and choose in “Default product sorting” to “Sort by most recent” option.

    Regards,
    Kris

    Thread Starter jmarvid

    (@jmarvid)

    Hi @c0nst :
    what you say it′s related to show products on woocommerce shop page but i′m talking about when you search for something on the Fibosearch text box.
    Listed results are sorted and being showed by relevance and i want to show them by date.
    Kind regards
    Jose

    @jmarvid
    You can use this code snippet:

    add_filter( 'dgwt/wcas/search_results/product/score', function( $score, $keyword, $post_id, $post ) {
    	$post_date = date( 'F j, Y', strtotime( $post->post_modified ) );
    	$today     = date( 'F j, Y' );
    	$diff      = abs( strtotime( $today ) - strtotime( $post_date ) );
    
    	if ( 0 === $diff ) {
    		$score = $score + 99999999;
    	} else {
    		$score = $score + ( 100 - ( $diff / 86400 ) );
    	}
    
    	return $score;
    }, 10, 4 );

    You have two ways to add this code to your theme:
    Open the functions.php in your child theme and add the code at the end.
    or install the Code Snippets plugin and apply this code as a snippet.

    Regards,
    Kris

    • This reply was modified 2 years, 10 months ago by Kris.
    Thread Starter jmarvid

    (@jmarvid)

    Hi @c0nst again,
    As you told me, i used a snippet to add your code to the website https://www.lasuite-online.com but if i do a search on the search box and press return i only get results ordered by popularity(relevance) not sorted by the date you create or update products.

    Hi @jmarvid

    This snippet adds some product scores to the products by their modified date (the closer the modification date is to today, the more points the product receives and is higher on the search results list) and the relevance sort actually displays products sorted by their modified date. So the results should be as you want.

    Regards,
    Kris

    • This reply was modified 2 years, 10 months ago by Kris.

    Hello
    How can I sort the results so that the products with the searched text in the product title appear first and by product publication date in descending order?
    The same thing that they have asked, but without it being the date of modification, but rather that of the publication of the product.

    Very thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Sort search results’ is closed to new replies.