Forums Forums Search & Filter Pro Filter by meta_query custom field

Tagged: ,

Viewing 10 posts - 1 through 10 (of 10 total)
  • Trevor
    #217704

    You do not appear to be setting that up as a function, per this documentation:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    And are you then placing it in the child theme function.php file?

    Anonymous
    #217706

    Yes it is a function, I was just showing you only the parts which you needed to see, here is it all:

    
    /**
     * USER PROFILE - Modify search form args
     */
    function user_liked_products_search_filter( $query_args, $sfid ) {
    
      if ( $sfid == 26 ) {
        $query_args = array (
          'meta_query' => array(
            array(
              'key'       => '_likes',
              'value'     => 1,
            ),
          ),
        );
      }
    
      return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\user_liked_products_search_filter', 100, 2 );
    
    Anonymous
    #217710

    It’s added to my functions.php file in the parent theme not child, I am building my own theme, no need for a child theme here.

    Trevor
    #217813

    Why is this:

    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\user_liked_products_search_filter', 100, 2 );

    Not this:

    add_filter( 'sf_edit_query_args', 'user_liked_products_search_filter', 100, 2 );

    Anonymous
    #217817

    Because I am in a namespaced file.

    Why PHP Namespaces

    In short, PHP namespaces address the problem of isolation. Before they were introduced to PHP, if you had a function, class, or constant called, say, Validation_Error in one part of your code, you simply couldn’t have another one like that anywhere else.

    Trevor
    #217823

    Assuming that _likes is the correct key name, I cannot see why this would not work.

    Anonymous
    #217825

    I am using the same filter to modify the main shop search too. This one works fine:

    
    function update_search_category( $query_args, $sfid ) {
    
      $query_args['tax_query'] = array(
        array(
          'taxonomy'      => 'product_cat',
          'terms'         => '20',
          'operator'      => 'IN',
        ),
      );
    
      return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\update_search_category', 20, 2 );
    
    Trevor
    #217829

    EXCEPT that there you are not restricting it to a form ID, which you should be?

    Anonymous
    #217831

    Currently I am not simply for dev purposes. However I just fixed the issue moments ago! That’s 2 fixes today for me, I am on a roll haha.

    Does not seem I was writing the meta_query correctly:

    Before:

    
    $meta_query = [
        'key'     => '_liked',
        'value'   => '1',
        'compare' => 'LIKE',
      ];
    
    $query_args['meta_query'] = $meta_query;
    

    After

    
    $query_args['meta_query'] = array(
        array(
          'key'     => '_liked',
          'value'   => '4',
          'compare' => 'LIKE',
        ),
      );
    

    For now decided to not do the declare at the top and then pull in below. This works so I’ll run with it for now.

    Thanks for the responses Trevor, always appreciated.

    Anonymous
    #217833

    I changed the value on that post but works regardless, can’t edit the last reply.

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