• Resolved rscarter1

    (@rscarter1)


    I would like the serach results to be ordered by a custom date field (auction_date_new). I use this same field to order the posts on archive pages. BUt it’s not working for the search page.

    I have tried the filter:

    add_filter( 'relevanssi_modify_wp_query', 'rlv_desc_date' );
    function rlv_desc_date( $query ) {
    	$query->set( 'meta_key', 'auction_date_new' );
        $query->set( 'order', 'DESC' );
    	$query->set( 'orderby', 'meta_value_date' );
        return $query;
    }

    and variations of it. It changes the order, but seemingly randomly. It’s not in order of date (newest to oldest).

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Relevanssi only understands meta_value and meta_value_num.

    This works on my test site:

    add_filter( 'relevanssi_modify_wp_query', 'rlv_desc_date' );
    function rlv_desc_date( $query ) {
    	$query->set( 'meta_key', 'auction_date_new' );
    	$query->set( 'order', 'desc' );
    	$query->set( 'orderby', 'meta_value' );
    	return $query;
    }

    (I used a YYYY-MM-DD date format. Do you use something else?)

    • This reply was modified 2 years, 6 months ago by Mikko Saari.
    Thread Starter rscarter1

    (@rscarter1)

    AH, I think I’m using MM-DD-YYYY
    I tried that but it didn’t work. Is there a way to convert strtotime directly in this filter?

    • This reply was modified 2 years, 6 months ago by rscarter1.
    Plugin Author Mikko Saari

    (@msaari)

    That’s going to be a tough one to sort by. If switching to “YYYY-MM-DD” is not possible, then one approach is to sort the posts in the relevanssi_hits_filter hook. See documentation. This hook gives you all the posts found in an array, and you can order the array any way you want to. You can run the posts through usort() using a comparison function that checks the date from the meta field and can handle “MM-DD-YYYY” dates.

    Thread Starter rscarter1

    (@rscarter1)

    I actually was able to change it and it works.
    It’s just the display (how the date is displayed) – it’s not pretty. lol
    I can probably find a way to convert it.

    Super slowmo high-five for the incredibly fast help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Order posts by custom date field’ is closed to new replies.