@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.