Forums Forums Search & Filter Pro Use meta query with Search & Filter Pro

Viewing 3 posts - 1 through 3 (of 3 total)
  • Anonymous
    #17796

    Hi, I use Search & Filter Pro plugin. It works very very good. I have some custom post types and I have setup some forms that allows the user to filter results when he is view an archive page. My Settings are as follows.

    Display results: As an archive page
    Use a custom template for results? Yes
    Enter the filename of the custom template: archive-expenses.php (my custom post type is called expenses)
    Load results using Ajax? Yes

    The problem is that I want to use an extra meta_query when filtering that it comes from the current user meta. To be specific I have a user meta (user_group) and I use the code below with the pre_get_posts hook but I cannot make it worh with the Search & Filter Pro plugin:

    $user_group = get_the_author_meta(‘user_group’, get_current_user_id());
    if($user_group != ‘administrator’){
    $meta_query = array(
    array(
    ‘key’ => ‘visits_user_group’,
    ‘value’ => $user_group,
    ‘compare’ => ‘=’
    )
    );
    $query->set( ‘meta_query’, $meta_query );
    }

    Any ideas? Thanks in advance!

    Ross Moderator
    #17804

    Hey Alexander

    Please see the following post as an example of hooking into Search & Filter queries:

    https://support.searchandfilter.com/forums/topic/complicated-custom-meta-field-search-dates-filtering/#post-11792

    This will be changing in S&F 2.0, as there are some new functions and considerations, however these will be well documented once they are ready.

    Thanks

    Anonymous
    #17957

    Hi, thanks for your quick reply !!!
    Ok I get make it work. In my code I make use pre_get_posts to filter the results at a given custom post type archive. The reason that it was not working was that when geting the query from filtering it does not work this code “$query->is_post_type_archive(‘expenses’)”.
    So I replaced this:
    if( $query->is_main_query() && !is_admin() && $query->is_post_type_archive(‘expenses’) ) { }
    with this:

    if( $query->is_main_query() && !is_admin() && ($query->is_post_type_archive(‘expenses’) || $sf_form_data->form_id()==1864) ) { }

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