Yes, you can’t do that, because if you have an extra wp_query on your search results page, Relevanssi doesn’t know where to attach itself. Blame WP for not providing a better API to replace the default search… Also, Relevanssi doesn’t have all the functionality that WP_Query has, and this is one of those things – even if you could pass the query to Relevanssi (which you can, actually), it wouldn’t do anything.
There are ways to do this, though. You can, for example, write a filter function on relevanssi_hits_filter
that removes the unwanted posts from the search results.
See:
Using relevanssi_hits_filter
Separating posts by post type
Thanks for the reply.
I’ve had a look at your to links but i’m not able to write a function myself.
If anyone knows how to filter by custom post types please paste examples here.
I need it to filter only ‘public’ post if user is NOT logged in and both ‘public’ and ‘private’
if they are. I have a custom field call access with two option.
thanks for your help people.
Try this:
add_filter('relevanssi_post_ok', 'customfield_ok_test');
function customfield_ok_test($post_ok, $post_id) {
$access = get_post_meta($post_id, 'access', true);
if ($access == 'private' && !is_user_logged_in()) return false;
return true;
}
Add this to your theme functions.php file. This should do the trick. This will allow all posts in search results, except those with access set to “private” when the user is not logged in.
Hmm, thanks so much for your help. but the private posts still show up in the search results.?
So you have a custom field “access” and the value of the field is either “public” or “private”, did I understand that correctly?
Looks like my code was maybe a bit wrong, try changing the first line to this:
add_filter('relevanssi_post_ok', 'customfield_ok_test', 10, 2);
Damn! you’ve done it! Thank you so much! really appreciate your support on this.
now I can use this awesome plugin! cheers
Sorry just one more thing, could this now be causing “Draft” post showing up in search results? thanks
Could be. Here’s a bit more precise version, then, which respects the default value of $post_ok as well (so private posts and drafts stay private, for example).
add_filter('relevanssi_post_ok', 'customfield_ok_test', 10, 2);
function customfield_ok_test($post_ok, $post_id) {
$access = get_post_meta($post_id, 'access', true);
if ($access == 'private' && !is_user_logged_in()) $post_ok = false;
if ($access == 'private' && is_user_logged_in()) $post_ok = true;
if ($access == 'public') $post_ok = true;
return $post_ok;
}
Hmmm no sorry draft post are still coming up in my search results for all logged in members.
I appreciated your help
I changed it to the following and it seems to work now.
add_filter('relevanssi_post_ok', 'customfield_ok_test', 10, 2);
function customfield_ok_test($post_ok, $post_id) {
$post_status = get_post_status($post_id);
$access = get_post_meta($post_id, 'access', true);
if ($access == 'private' && !is_user_logged_in() && !$post_status == 'draft') $post_ok = false;
if ($access == 'private' && is_user_logged_in() && !$post_status == 'draft') $post_ok = true;
if ($access == 'public') $post_ok = true;
return $post_ok;
}
Is there a better way to do this?
Again thankyou for your time!
Ah, yes, the code I sent will probably show drafts to logged in members, I didn’t realize that was a requirement as well. If that’s what you want, then yes, your code is probably the best way to handle it.
Hi everybody,
Does anyone know how to filter by post-id so the admin can search by the number of the post, please paste examples here…
Thanks !