Forums Forums Search & Filter Pro A third sort order?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Anonymous
    #242726

    Is it possible to control the sort order beyond the two sort order options available in the Posts tab in the searchandfilter settings?

    We can’t use relevanssi because we have 12,000 posts.

    I have set the search to sort by one meta value first (subject_code), and then another (course_number). After that, I’d like it to sort by a third meta value (start_date). Can I programmatically add a third somehow using an action hook?

    Or, at the very least could I have it sort alphabetically after the first two?

    Thank you!

    Trevor
    #242759

    You would need to return the sort order settings in the Posts tab back to default, and instead see if you can use this filter:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    This filter uses the same argument structure as wp_query.

    Anonymous
    #242855

    Thank you. I restored the Posts settings to the default order and used the following code, which is not affecting the sort order:

    
    function filter_function_name( $query_args, $sfid ) {
    	
    	if($sfid==23142) {
    	
    		$query_args["orderby"] = array(
            	'subject_code'       => 'asc',
            	'course_number'       => 'asc',
    			'start_date'     => 'asc',
    		);				
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    It works if I set just a single order like so:

    
    function filter_function_name( $query_args, $sfid ) {
    	
    	if($sfid==23142) {
    	
    		$query_args["orderby"] = 'subject_code';		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Do I need to format the $query_args differently? That’s how I do it in a regular WP_Query…

    Thank you!

    Anonymous
    #242872

    Nevermind. I didn’t realize I had to also manually add the meta_query to get the search order to work. Here is my working code:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==23142) {
    		
    	     $query_args["meta_query"] = array(
    	        'relation' => 'AND',
    	        'subject_code' => array(
                    'key'       => 'subject_code',
                    'compare'   => 'EXISTS',
                ),
                'course_number' => array(
                    'key'       => 'course_number',
                    'compare'   => 'EXISTS',
                ),
                'start_date' => array(
                    'key'       => 'start_date',
    		'compare'   => 'EXISTS',
                ),
    	    );
    	    
    		$query_args["orderby"] = array(
            	'subject_code'       => 'asc',
            	'course_number'       => 'asc',
            	'start_date'       => 'asc',
    		);				
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    

    Please feel free to close this ticket. Thank you!

    Trevor
    #242881

    Thanks for sharing. I will close this thread for now.

Viewing 5 posts - 1 through 5 (of 5 total)