if ($somevariable=='true') {
display custom results where parent ID = 123
} else {
display normal relevanssi results
};
There’s the WordPress query parameter post_parent, which would be the easy way to do this, but Relevanssi doesn’t support it – you’re the first one to ever ask about it. Maybe I’ll add it in a future version.
Meanwhile, you’re best off using relevanssi_hits_filter. Something like this (I didn’t test it so no guarantees it actually works, but you get the idea):
add_filter('relevanssi_hits_filter', 'rlv_post_parent', 10);
function rlv_post_parent($hits) {
global $wp_query;
if (isset($wp_query->query_vars['somevariable'])) {
$valid_posts = array();
foreach ($hits[0] as $hit) {
if ($hit->post_parent == 123) $valid_posts[] = $hit;
}
$hits[0] = $valid_posts;
}
return $hits;
}
Something like this. You need to introduce your query variable to WordPress with query_vars, otherwise WordPress wil clean it out.
I don’t seem to be able to make it work. I think the code is as you’ve explained. Ive updated with my own actual variables.
function themeslug_query_vars( $searchdept ) {
$searchdept[] = 'custom_query_var';
return $searchdept;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
add_filter('relevanssi_hits_filter', 'rlv_post_parent', 10);
function rlv_post_parent($hits) {
global $wp_query;
if (isset($wp_query->query_vars['searchdept'])) {
$valid_posts = array();
foreach ($hits[0] as $hit) {
if ($hit->post_parent == '32') $valid_posts[] = $hit;
}
$hits[0] = $valid_posts;
}
return $hits;
}
The search url is as follows http://mysite.com/?s=search+term&searchdept=true
Any ideas?
I’d start debugging this piece by piece. Is the code running? Is the searchdept parameter available? Does the post_parent if trigger? Where does the code fail?
You are adding this code to the function.php file correct?
If I change return $hits; to echo 'test';
Test is displayed on the page so the function is running.
If I var_dump $hits, it displays the array but that array includes things other than posts with a parent of 32.
It seems as though this bit of code is not true
if (isset($wp_query->query_vars['searchdept'])) {
$valid_posts = array();
foreach ($hits[0] as $hit) {
if ($hit->post_parent == '32') $valid_posts[] = $hit;
}
echo '<h1>test</h1>';
}
I have no idea why.
Yup, If I remove this condition it works.
if (isset($wp_query->query_vars['searchdept'])) {
Strange. The variable is in the url with a value. Any ideas?
Changing that condition to this worked
if ($_GET["searchdept"] == "true") {
Ah, this is incorrect:
function themeslug_query_vars( $searchdept ) {
$searchdept[] = 'custom_query_var';
return $searchdept;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
It should be:
function themeslug_query_vars( $qv ) {
$qv[] = 'searchdept';
return $qv
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );
Now the original code should work as well.
ugh, that was s dumb mistake. late night. thanks again.
Im passing the searchdept value through a hidden field in the search form so, I guess I dont need that portion of the code anymore since the GET method works.