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

    (@msaari)

    Anything is possible when using the relevanssi_hit_filter filter hook. Simply create a function that removes all child posts from the search results.

    Thread Starter xave

    (@xave)

    Hi

    I’ve been unsuccessful writing the filter.

    add_filter('relevanssi_modify_wp_query', 'mod_q');
    function mod_q($wp_query) {
    	if (is_search()) {
    		global $wp_query;
    		$wp_query->query_vars['post_parent'] = 0;
    	}
    	return $wp_query;
    }

    Could you offer any guidence to simply filter out any child posts from a search.
    many thanks

    Plugin Author Mikko Saari

    (@msaari)

    Relevanssi does not care about $wp_query->query_vars[‘post_parent’].

    Like I said, relevanssi_hits_filter is your friend here:

    add_filter('relevanssi_hits_filter', 'no_kids_allowed');
    function no_kids_allowed($hits) {
        $all_children_left_behind = array();
        foreach ($hits[0] as $post) {
            if ($post->post_parent == 0) $all_children_left_behind[] = $post;
        }
        return $all_children_left_behind;
    }
    Thread Starter xave

    (@xave)

    Thanks Mikko –
    However, the filter is returning error:

    PHP Fatal error: Cannot use object of type stdClass as array in …/relevanssi/lib/search.php on line 918

    Thread Starter xave

    (@xave)

    Fixed: Seems I just needed to return $all_children_left_behind as an array!

    add_filter('relevanssi_hits_filter', 'no_kids_allowed');
    function no_kids_allowed($hits) {
        $all_children_left_behind = array();
        foreach ($hits[0] as $post) {
            if ($post->post_parent == 0) $all_children_left_behind[] = $post;
        }
        return array($all_children_left_behind);
    }

    Many thanks – saved the day!

    Plugin Author Mikko Saari

    (@msaari)

    Ah, yes, sorry about that – the filter does expect the result in an array. Glad you got it sorted out.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Exclude child posts’ is closed to new replies.