You can do this with a combination of rewrite endpoints and the pre_get_posts
action.
First, your taxonomy has to be registered with some specific rewrite
arguments to enable this, particularly, ep_mask
:
register_taxonomy(
'customtax',
'posttype',
array(
'rewrite' => array(
'slug' => 'custom-tax',
'hierarchical' => true,
'ep_mask' => EP_ALL
),
// other args...
)
);
You can then add an endpoint for your filter, also on the init
action:
add_rewrite_endpoint( 'low-price', EP_ALL );
Note that rewrite rules need to be flushed at this point for the new endpoint to exist.
The last step is to then hook pre_get_posts
and detect the low-price
endpoint, and apply the proper meta arguments to the query:
function wpd_filter_my_tax( $query ) {
if ( $query->is_tax( 'customtax' )
&& $query->is_main_query()
&& isset( $query->query_vars['low-price'] ) ) {
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'wpd_filter_my_tax' );