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

Add Sort by that filter by date newest or oldest

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Add Sort by that filter by date newest or oldest

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #57147
    purchases_Ctxhpurchases_Ctxh
    Participant

    I was able to add the post/category drop down, but I would also like a ‘Sort by’ that has dates newest and oldest options (based on the publish dates) – I read the documentation, but it isn’t clear to me how to implement this.

    #57149
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    While currently there is no out of the box filter for that, it is still possible to achieve it via a custom field filter + a bit of a custom code.

    Follow the steps below:

    1. Create a drop-down custom field filter for a non-existint field custom_order with these values (see this screenshot):

    date||Date ASC
    date_reverse||Date DESC

    2. Add this code via the Code Snippets plugin or to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines:

    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',
    			'price',
    			'price_reverse',
    		);
    
    		$ordering = '';
    
    		foreach ( $args['post_meta_filter'] as $k => $filter ) {
    			if ( $filter['key'] === 'custom_order' ) {
    				$ordering = $filter['value'];
    				unset( $args['post_meta_filter'][$k] );
    			}
    		}
    
    		if ( $ordering === '' || !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 ASC';
    				break;
    			case 'alpha_reverse':
    				$args['post_primary_order'] = 'post_title DESC';
    				break;
    			case 'price':
    				$args['post_primary_order']          = 'customfp ASC';
    				$args['post_primary_order_metatype'] = 'numeric';
    				$args['_post_primary_order_metakey'] = '_price';
    				break;
    			case 'price_reverse':
    				$args['post_primary_order']          = 'customfp DESC';
    				$args['post_primary_order_metatype'] = 'numeric';
    				$args['_post_primary_order_metakey'] = '_price';
    				break;
    			case 'relevance':
    			default:
    				$args['post_primary_order'] = 'relevance DESC';
    				break;
    		}
    
    		return $args;
    	}
    );

    This will convert the custom field filter into a sorting filter. In case you need help with it, let me know 🙂

    #57150
    purchases_Ctxhpurchases_Ctxh
    Participant

    Thank you – I have added this to the functions and plugin – I switched from post category search to a custom post type / taxonomy approach. I noticed that I don’t have a multi-select dropdown option though – only multi-select/search option. Is there a way to apply a multi-select dropdown? Also, will/can the default view be to show all assets initially?
    I also noticed that the main category doesn’t seem to filter back after selecting the sub-category. I only have one item in place now, but I would expect that to show for both the parent and sub selected.

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