• Resolved robRalston1

    (@robralston1)


    So from what I’ve read and tried I am unable to do a wp_query on my search results page when using Relevanssi.

    I really need exclude all posts/pages that have a certain custom field value. on all other pages I can do this simply using wp_query or WP_Query. I tried the following custom function but it didnt seem to work.

    /

    This seem like something i;d be able to use f I could get it to work. I have two custom radio buttons with public and private options. how can I exclude all posts with the radio button set to private. with out using a wp_query on the results page.

    thanks guys

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

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

    (@msaari)

    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

    Thread Starter robRalston1

    (@robralston1)

    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.

    Plugin Author Mikko Saari

    (@msaari)

    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.

    Thread Starter robRalston1

    (@robralston1)

    Hmm, thanks so much for your help. but the private posts still show up in the search results.?

    Plugin Author Mikko Saari

    (@msaari)

    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);

    Thread Starter robRalston1

    (@robralston1)

    Damn! you’ve done it! Thank you so much! really appreciate your support on this.
    now I can use this awesome plugin! cheers

    Thread Starter robRalston1

    (@robralston1)

    Sorry just one more thing, could this now be causing “Draft” post showing up in search results? thanks

    Plugin Author Mikko Saari

    (@msaari)

    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;
    }
    Thread Starter robRalston1

    (@robralston1)

    Hmmm no sorry draft post are still coming up in my search results for all logged in members.

    I appreciated your help

    Thread Starter robRalston1

    (@robralston1)

    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!

    Plugin Author Mikko Saari

    (@msaari)

    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 !

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Plugin: Relevanssi – A Better Search] Excluding post with custom field value’ is closed to new replies.