Title: Exclude child posts
Last modified: August 20, 2016

---

# Exclude child posts

 *  Resolved [xave](https://wordpress.org/support/users/xave/)
 * (@xave)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/exclude-child-posts/)
 * Is it possible to exclude child posts of a custom post type from search results.
   
   ie only show results of pages and posts and custom post type where parent = 0
 * many thanks.
 * [http://wordpress.org/extend/plugins/relevanssi/](http://wordpress.org/extend/plugins/relevanssi/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294533)
 * 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](https://wordpress.org/support/users/xave/)
 * (@xave)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294558)
 * 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](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294559)
 * 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](https://wordpress.org/support/users/xave/)
 * (@xave)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294560)
 * 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](https://wordpress.org/support/users/xave/)
 * (@xave)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294561)
 * 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](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294562)
 * 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.

 * ![](https://ps.w.org/relevanssi/assets/icon-256x256.png?rev=3529670)
 * [Relevanssi - A Better Search](https://wordpress.org/plugins/relevanssi/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/relevanssi/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/relevanssi/)
 * [Active Topics](https://wordpress.org/support/plugin/relevanssi/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/relevanssi/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/relevanssi/reviews/)

## Tags

 * [child](https://wordpress.org/support/topic-tag/child/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [exclude](https://wordpress.org/support/topic-tag/exclude/)

 * 6 replies
 * 2 participants
 * Last reply from: [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/exclude-child-posts/#post-3294562)
 * Status: resolved