Forums Forums Search & Filter Pro sf_filter_query_args (Not working if I want to filter)

Viewing 10 posts - 1 through 10 (of 11 total)
  • Anonymous
    #127398

    Hello guys!

    I have a custom sf_filter_query_args like so:

    function sf_filter_query_args($query_args, $sfid) {
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 2);

    Which works perfectly when I come on the page. It filters succesfully on the meta_query. But when I select a filter it returns all result and not the results where likes_%_gebruiker is 16 + the filters.

    Am i doing something wrong? Can someone help me out?

    Thanks

    Trevor
    #127421

    You need to output the $query_args array before and after this code, like this:

    echo '<pre>',print_r($query_args,true),'</pre>';
    

    To see if any change was made. It may be that you need to run the code later, e.g.

    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 200);
    
    Anonymous
    #127444

    Hi Trevor,

    I made it so the code runs later and tested multiple values but I dont get it to work. See what is returns:

    Example

    It only has to show the one with the red hearth, it works on page load but not when I select another filter (Speeltuin in example) (It works when I refresh the page with the filter on though, than it shows only the one with red hearth).

    Any ideas?

    Thanks!

    Anonymous
    #127446

    Example

    Trevor
    #127448

    What I would like to see is the before and after contents from the print_r.

    Anonymous
    #127457

    Oops, sorry it is:

    Array
    (
    )
    Array
    (
        [meta_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [key] => likes_%_gebruiker
                        [value] => 16
                        [compare] => LIKE
                    )
    
            )
    
    )
    Trevor
    #127459

    When you have made a filter selection, is this what it looks like before or after your code?

    Anonymous
    #127591

    Yes it is, the code is as following:

    function sf_filter_query_args($query_args, $sfid) {
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where_likes');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 400);
    Trevor
    #127623

    What I was trying to establish was …

    You arrive to the page and your code works. You show this with the two arrays shown in post 127457, where the fist array is empty, and then after your code has contents.

    Then you make a filter selection, and the filter selection works, but it ignores your code.

    So, when it does ignore your code, it should have run through your code, so on THAT page, what do the before and after arrays look like? Or do they not print out at all?

    Anonymous
    #127918

    Sorry for my late answer. I think the problem is because it loads the posts using AJAX so it skips my array. If I select a filter it changed the URL using jQuery to https://url.com/favorieten/?_sfm_filter_locatie=rondom and shows all the “rondom” posts so it skips my array.

    If I refresh that URL it works because than the posts does not get loaded using AJAX I guess?

Viewing 10 posts - 1 through 10 (of 11 total)