Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Filter custom query with meta_query

Tagged: 

Viewing 4 posts - 1 through 4 (of 4 total)
  • Eva Brarens
    #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 Moderator
    #259361

    Ordering 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/

    Eva Brarens
    #259910

    Thanks! 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)

    Trevor Moderator
    #259914

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

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

The topic ‘Filter custom query with meta_query’ is closed to new replies.