Filter custom query with meta_query

Forums › Forums › Search & Filter Pro › Filter custom query with meta_query

Tagged: 

Viewing 4 posts – 1 through 4 (of 4 total)
  • Anonymous
    #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
    #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/

    Anonymous
    #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
    #259914

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

Viewing 4 posts – 1 through 4 (of 4 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.