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

    (@msaari)

    I don’t know for sure what pod fields are, but I assume they are stored in custom fields.

    Here are some custom field methods. This is easiest with Premium, where you can directly see which custom field has the search term and you can set the match weight to 0 unless the search term appears in the correct custom field.

    Without Premium, it’s also possible, but instead of looking at the $match->customfield_detail, you need to get the actual custom field value and see if the term appears there. This works fine, but does mean you’ll be making a lot more database queries, so the performance may suffer.

    To filter by taxonomy, see tax_query.

    Thread Starter allanext

    (@allanext)

    Thank you @msaari !

    we are def purchasing the pro version, also considering the indexing of epubs and pdfs. And you are correct; Pods are like ACF or Toolset for creating content types, but open source.

    If I understand correctly the relevanssi_match hook is for filtering the results returned by the query (eventually setting the weight to 0 to not show them?), but wouldn’t it be better to add filters to the query like this ?

    In this stackoverflow there’s a full example that can be very helpful at first when trying to understand how to integrate all the parts. The second answer.

    Can the taxonomy filters be integrated in the relevanssi_modify_wp_query hook as well? From your link I don’t see how to integrate the extra taxonomy arguments in the WP_Query/searchform.php

    Thanks again!

    • This reply was modified 3 years, 11 months ago by allanext.
    • This reply was modified 3 years, 11 months ago by allanext.
    Plugin Author Mikko Saari

    (@msaari)

    Yes, relevanssi_match is for filtering out results, but there are some things you just can’t filter in the query.

    Custom fields are one: you can throw in a meta_query, but you can’t do a meta_query for “only match this search term if it appears in a specific custom field”. (You can do “only return posts with this word appearing in this custom field”, but a) you don’t need Relevanssi for that and b) it’s not the same.) The relevanssi_match and relevanssi_hits_filter filter hooks let you do more complicated filtering.

    You don’t do the taxonomy parameters in searchform.php, no. You throw them in in the relevanssi_modify_wp_query hook or in pre_get_posts by adding a tax_query element in the query.

    If you want the tax_query to be controlled by something the user does in the search form, you add a field in the search form, and then add the tax_query based on the value user provided in relevanssi_modify_wp_query (it’s better to use this than pre_get_posts, saves you the trouble of figuring out if you’re in a search query or not). There’s no way to add a complicated tax_query directly from the search form. Simple taxonomy parameters can be added, like a category dropdown filter.

    Thread Starter allanext

    (@allanext)

    @msaari thank you for the tips!

    here’s an example of the code in function.php for the above functionality:

    add_filter('query_vars', 'rlv_add_qv');
      function rlv_add_qv($qv) {
        $qv[] = 'subjects';
        $qv[] = 'geluk';
        $qv[] = 'kagyu';
        $qv[] = 'nyingma';
        $qv[] = 'sakya';
        return $qv;
      }
    
      add_filter('relevanssi_modify_wp_query', 'rlv_add_meta_query');
      function rlv_add_meta_query($query) {
    
        //echo var_dump($query->query_vars);
        $subject = isset($query->query_vars['subjects']) && !empty($query->query_vars['subjects'] && $query->query_vars['subjects'] != "any");
        $kagyu = isset($query->query_vars['kagyu']) && !empty($query->query_vars['kagyu']);
        $nyingma = isset($query->query_vars['nyingma']) && !empty($query->query_vars['nyingma']);
        $sakya = isset($query->query_vars['sakya']) && !empty($query->query_vars['sakya']);
        $geluk = isset($query->query_vars['geluk']) && !empty($query->query_vars['geluk']);
    
        if ($kagyu || $nyingma || $sakya || $geluk) {
          
          global $wpdb;
          if ($kagyu) {
            $tax_school[] = array(
              'taxonomy' => 'school',
              'field'    => 'name',
              'terms'    => 'kagyu',
            );
          }
          if ($geluk) {
            $tax_school[] = array(
              'taxonomy' => 'school',
              'field'    => 'name',
              'terms'    => 'geluk',
            );
          }
          if ($nyingma) {
            $tax_school[] = array(
              'taxonomy' => 'school',
              'field'    => 'name',
              'terms'    => 'nyingma',
            );
          }
          if ($sakya) {
            $tax_school[] = array(
              'taxonomy' => 'school',
              'field'    => 'name',
              'terms'    => 'sakya',
            );
          }
          if (count($tax_school) > 1) {
            $schools = array('relation' => 'OR');
            $schools = array_merge($schools, $tax_school);
          } else if (count($tax_school) == 1){
            $schools = $tax_school;
          }
        }
        $subject_val = $query->query_vars['subjects'];
          
        if ($subject_val != "any" && !($kagyu || $nyingma || $sakya || $geluk)) {
          $tax_query[] = array(
            'taxonomy' => 'subject',
            'field'    => 'name',
            'terms'    => $subject_val,
          );
        } else if ($subject_val != "any" && ($kagyu || $nyingma || $sakya || $geluk)) {
          $tax_query = array(
            'relation' => 'AND',
            array(
              'taxonomy' => 'subject',
              'field'    => 'name',
              'terms'    => $subject_val,
            ),
            $schools
          );
        } else if ($subject_val == "any" && ($kagyu || $nyingma || $sakya || $geluk)) {
          $tax_query = $schools;
        }
        //echo "\nTAX QUERY: \n";
        //echo '<pre>' .var_export($tax_query, true) . '</pre>';
    
        $query->set( 'tax_query', $tax_query ) ;      
        return $query;
      }
    
      
      add_filter( 'relevanssi_match', 'custom_field_weights' );
      function custom_field_weights( $match ) {
    
        if (isset($_GET['search-book-title'])) {
          $only_title = $_GET['search-book-title'];
          $custom_field_detail = json_decode( $match->customfield_detail );
          //echo '<pre>' .var_export($only_title, true) . '</pre>';
          if ( null === $custom_field_detail || 
            !(isset($custom_field_detail->book_title) || isset($custom_field_detail->book_title_english))) {
            $match->weight = 0;
          }
        }
        return $match;
      }

    Note that on the relevanssi_match I accessed the searchform parameters with the $_GET variable instead of the $query->query_vars

    Thank you!

    • This reply was modified 3 years, 11 months ago by allanext.
    • This reply was modified 3 years, 11 months ago by allanext.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Relevanssi with live searchWP and Pods fields as filters’ is closed to new replies.