Forums › Forums › Search & Filter Pro › Filter custom query with meta_query
Tagged: custom query
- This topic has 3 replies, 2 voices, and was last updated 4 years, 2 months ago by Trevor.
-
Anonymous(Private) September 14, 2020 at 4:04 pm #259356
Hi,
I’m trying to add a filter to a custom query for a custom post type, sorted on a meta field. This works fine until I add a secondary sort order (the query sorts on a date field and then on a time field), then the query order jumps to the default – post date ASC. The filter still works so that’s not the issue, it’s that it breaks my query sort order.
The custom query $args looks like this:
$args = array(
‘post_type’ => ‘myCPT’,
‘paged’ => $paged,
‘meta_query’ => array(
‘relation’ => ‘AND’,
‘date_clause’ => array(
‘key’ => ‘myCustomDateField’,
‘value’ => $today,
‘compare’ => ‘>=’
),
‘time_clause’ => array(
‘key’ => ‘myCustomTimeField’,
‘compare’ => ‘EXISTS’,
),
),
‘orderby’ => array(
‘date_clause’ => ‘ASC’,
‘time_clause’ => ‘ASC’,
)
);
$args[‘search_filter_id’] = 1;Is this expected behavior (as in: it’s outside the scope of the plugin) or am I doing something wrong?
Thanks
Trevor(Private) September 14, 2020 at 4:54 pm #259361Ordering is (normally) handled within our plugin, so any query arguments you set for ordering may be ignored/overridden. Instead, you would need to add them using this filter:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
I am not sure if that would work, but this forum search should yield some code snippets:
https://support.searchandfilter.com/forums/search/orderby+function+sf_edit_query_args/
Anonymous(Private) September 17, 2020 at 12:01 pm #259910Thanks! I solved it per the documentation you linked and the code example here: https://support.searchandfilter.com/forums/topic/a-third-sort-order/ (In case anyone is ever looking for the same answer)
-
AuthorPosts