Support » Plugin: Relevanssi - A Better Search » Filter WP_Query with custom field

  • Hello Mikko and others who read this,

    I have been using Relevanssi almost in all my projects, but now for current project I need some functionality that I think is only achievable with premium version of Relevanssi, so I would love if you could just confirm that for me.

    This is the code that is working right now that does not have any filtering using custom fields: http://pastebin.com/KbeK3CsF

    What I want is to filter $products query by some value of custom field called “product_is_preowned”.

    So I have tried to do it like this http://pastebin.com/KpD342Tw the change is on line 22.

    Is that possible with premium Relevanssi or maybe I can do it somehow with free version?

    Thanks,
    Miloš

    http://wordpress.org/plugins/relevanssi/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    As answered in the email: Premium is not necessary, this can be done with filters.

    Thread Starter Miloš Gavrilovi?

    (@gavra)

    Yup, this is what I ended up using thanks to your suggestion:

    add_filter('relevanssi_modify_wp_query', 'modify_product_search');
    
    function modify_product_search( $wp_query ) {
    
      // Get post type
      if ( ! empty( $_GET['type'] ) ) {
        $type = htmlentities( $_GET['type'] );
      }
    
      if ( $type != 'articles' ) {
        // Default posts per page
        $posts_per_page = 12;
    
        // If post type is products
        // set posts per page to -1
        if( $type == 'products' ) {
          $posts_per_page = -1;
        }
    
        $wp_query->query_vars['posts_per_page'] = $posts_per_page;
        $wp_query->query_vars['post_type']      = 'product';
        $wp_query->query_vars['meta_key']       = 'product_is_preowned';
        $wp_query->query_vars['meta_value']     = 'no';
      }
    
      return $wp_query;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Filter WP_Query with custom field’ is closed to new replies.