This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Implement a sorting dropdown

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Implement a sorting dropdown

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #55580
    devopstools_sPAZdevopstools_sPAZ
    Participant

    Hello

    We need to implement sort by date ascending/descending besides alphabetical sort.

    It seems the sort functionality is not shipped with the plugin by default.

    Can you please confirm. and if not is there a custom way to implement it (if so please attach related documentation)

    Best Regards

    #55583
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    I’m afraid this is not possible, there is no sorting filter available as of now for the live results.

    I usually recommend a customization or a custom code where possible, but this is a lot more complicated than that. The only way to achieve this is to fully implement it from top to bottom, which unfortunately takes more than a few lines of code.

    It will be available though in a later release, as it is a planned feature within the filters section rework.

    #55586
    devopstools_sPAZdevopstools_sPAZ
    Participant

    thanks for your response.

    if we created the date sort dropdown and made it adding the query parameters to the URL.

    is there a way to hack into the search query to add the sorting parameters.

    Best

    #55592
    Ernest MarcinkoErnest Marcinko
    Keymaster

    On the search results page it is actually possible without any additional code, as it will recognize the $_GET['order'] and $_GET['orderby'] parameters.

    All you need to do is append that to the end of URL as query parameters, like: ...&order=ASC&orderby=id

    The possible supported values by default are:
    order parameter: ASC, DESC
    orderby parameter: id, post_id, product_id, popularity, post_popularity, product_popularity, rating, post_rating, product_rating, date, post_date, product_date, name, post_name, alphabetical, price, relevance

    I suggest using these if you can. If you want you can custom code additional accepted values as you like, let me know and I will let you know a custom code snippet.

    #55602
    devopstools_sPAZdevopstools_sPAZ
    Participant

    Yes if you can provide me with a code snippet will help a lot.
    Much appreciated.

    #55605
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Okay.

    So say you have a single drop-down, which appends the “the_order” query variable, so the URL looks like this “…&the_order=yourvalue” on the results page. Then it is accessed on page load within the code via the $_GET['the_order'].

    Here is a custom code snippet which handles that, it has examples for id, date, title, relevance and even string and numeric custom field based ordering:

    // Custom ordering
    add_filter(
    	'asp_query_args',
    	function ( $args ) {
    		// The values allowed in the the_ordering variable
    		$allowed_orderings = array(
    			'relevance',
    			'id',
    			'id_reverse',
    			'date',
    			'date_reverse',
    			'alpha',
    			'alpha_reverse',
    			'numeric_custom_field',
    			'string_custom_field',
    		);
    
    		if ( !isset($_GET['the_order']) ) {
    			return $args;
    		}
    
    		$ordering = sanitize_text_field( wp_unslash( $_GET['the_order'] ) );
    		if ( !in_array($ordering, $allowed_orderings, true) ) {
    			return $args;
    		}
    
    		switch ( $ordering ) {
    			case 'id':
    				$args['post_primary_order'] = 'id DESC';
    				break;
    			case 'id_reverse':
    				$args['post_primary_order'] = 'id ASC';
    				break;
    			case 'date':
    				$args['post_primary_order'] = 'post_date DESC';
    				break;
    			case 'date_reverse':
    				$args['post_primary_order'] = 'post_date ASC';
    				break;
    			case 'alpha':
    				$args['post_primary_order'] = 'post_title DESC';
    				break;
    			case 'alpha_reverse':
    				$args['post_primary_order'] = 'post_title ASC';
    				break;
    			case 'numeric_custom_field':
    				$args['post_primary_order']          = 'customfp DESC';
    				$args['post_primary_order_metatype'] = 'numeric';
    				$args['_post_primary_order_metakey'] = 'custom_field_name';
    				break;
    			case 'string_custom_field':
    				$args['post_primary_order']          = 'customfp DESC';
    				$args['post_primary_order_metatype'] = 'string';
    				$args['_post_primary_order_metakey'] = 'custom_field_name';
    				break;
    			case 'relevance':
    			default:
    				$args['post_primary_order'] = 'relevance DESC';
    				break;
    		}
    
    		return $args;
    	}
    );
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.