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 by meta_query custom field

Tagged: ,

Viewing 10 posts - 1 through 10 (of 11 total)
  • Jordan
    #217681

    I have checked every similar thread regarding this but am still stuck it seems:

    I’m trying to filter the products on my S&F form to display only those products with a certain number of ‘LIKES’.

    
      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 );
    

    The function itself is running and also definitely against form ID 26. However it doesn’t appear in the S&F query it seems, here is a dump of the following:

    
     global $searchandfilter;
     $sf_current_query = $searchandfilter->get(26)->current_query();
     var_dump($sf_current_query);
    
    
    object(Search_Filter_Active_Query)[2753]
      public 'sfid' => int 26
      private 'is_set' => boolean true
      private 'query_array' => 
        array (size=0)
          empty
      private 'form_fields' => 
        array (size=2)
          'search' => 
            array (size=4)
              'type' => string 'search' (length=6)
              'heading' => string '' (length=0)
              'placeholder' => string 'Search …' (length=10)
              'accessibility_label' => string '' (length=0)
          '_sft_product_cat' => 
            array (size=18)
              'type' => string 'taxonomy' (length=8)
              'input_type' => string 'select' (length=6)
              'heading' => string '' (length=0)
              'accessibility_label' => string '' (length=0)
              'all_items_label' => string '' (length=0)
              'show_count' => int 1
              'hide_empty' => int 1
              'hierarchical' => int 1
              'include_children' => int 0
              'drill_down' => int 0
              'sync_include_exclude' => int 1
              'combo_box' => int 1
              'no_results_message' => string '' (length=0)
              'operator' => string 'and' (length=3)
              'order_by' => string 'default' (length=7)
              'order_dir' => string 'asc' (length=3)
              'exclude_ids' => string '' (length=0)
              'taxonomy_name' => string 'product_cat' (length=11)
      private 'query_str' => int -1
      public 'plugin_slug' => string 'search-filter' (length=13)
      public 'form_settings' => 
        array (size=37)
          'use_template_manual_toggle' => int 1
          'enable_taxonomy_archives' => int 0
          'enable_auto_count' => int 0
          'auto_count_refresh_mode' => int 1
          'auto_count_deselect_emtpy' => int 0
          'template_name_manual' => string 'search.php' (length=10)
          'page_slug' => string '' (length=0)
          'post_types' => 
            array (size=1)
              'product' => int 1
          'post_status' => 
            array (size=1)
              'publish' => int 1
          'use_ajax_toggle' => int 1
          'scroll_to_pos' => string '0' (length=1)
          'pagination_type' => string 'normal' (length=6)
          'custom_scroll_to' => string '' (length=0)
          'auto_submit' => int 1
          'display_results_as' => string 'shortcode' (length=9)
          'update_ajax_url' => int 1
          'only_results_ajax' => int 1
          'ajax_target' => string '#main' (length=5)
          'ajax_links_selector' => string '.pagination a' (length=13)
          'infinite_scroll_container' => string '' (length=0)
          'infinite_scroll_trigger' => string '-100' (length=4)
          'infinite_scroll_result_class' => string '' (length=0)
          'show_infinite_scroll_loader' => int 1
          'results_per_page' => int 10
          'exclude_post_ids' => string '' (length=0)
          'field_relation' => string 'or' (length=2)
          'default_sort_by' => string '0' (length=1)
          'sticky_posts' => string '' (length=0)
          'default_sort_dir' => string 'desc' (length=4)
          'default_meta_key' => string '_um_core' (length=8)
          'default_sort_type' => string 'numeric' (length=7)
          'secondary_sort_by' => string '0' (length=1)
          'secondary_sort_dir' => string 'desc' (length=4)
          'secondary_meta_key' => string '_um_core' (length=8)
          'secondary_sort_type' => string 'numeric' (length=7)
          'taxonomies_settings' => 
            array (size=11)
              'category' => 
                array (size=2)
                  ...
              'post_tag' => 
                array (size=2)
                  ...
              'post_format' => 
                array (size=2)
                  ...
              'action-group' => 
                array (size=2)
                  ...
              'um_hashtag' => 
                array (size=2)
                  ...
              'product_type' => 
                array (size=2)
                  ...
              'product_visibility' => 
                array (size=2)
                  ...
              'product_cat' => 
                array (size=2)
                  ...
              'product_tag' => 
                array (size=2)
                  ...
              'product_shipping_class' => 
                array (size=2)
                  ...
              'ep_custom_result' => 
                array (size=2)
                  ...
          'results_url' => string 'https://dev.influxshopping.co.uk/user/admin/?profiletab=products' (length=64)
      public 'cache_table_name' => string 'wp_search_filter_cache' (length=22)
      public 'term_results_table_name' => string 'wp_search_filter_term_results' (length=29)
      public 'field_values' => 
        array (size=0)
          empty
    

    No sign of the _likes field in the query at all.

    Trevor Moderator
    #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?

    Jordan
    #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 );
    
    Jordan
    #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 Moderator
    #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 );

    Jordan
    #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 Moderator
    #217823

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

    Jordan
    #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 Moderator
    #217829

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

    Jordan
    #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.

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

The topic ‘Filter by meta_query custom field’ is closed to new replies.