WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
Filtering by post type after using 'seperating posts by (3 posts)

  1. jetlej
    Member
    Posted 2 years ago #

    I'm using the snippet you provided here: http://www.relevanssi.com/knowledge-base/separating-posts-by-post-type/

    ...but now when I add a 'post_type=custom_post_type' to the search query, no posts are returned. This is only the case when filtering for a custom post type.

    Any ideas? Thank you!

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

  2. jetlej
    Member
    Posted 2 years ago #

    Also, I don't need that post separation if a post_type parameter is supplied. So if an if statement can be applied to that code in functions.php, that would work too :)

  3. jetlej
    Member
    Posted 2 years ago #

    This code worked for me (provided by the developer). Just added this conditional around the original snippet:

    global $wp_query;
    	if ($wp_query->query_vars['post_type'] != 'post') {

    So the whole thing looks like this:

    add_filter('relevanssi_hits_filter', 'separate_result_types');
    function separate_result_types($hits) {
    
    	global $wp_query;
    	if ($wp_query->query_vars['post_type'] != 'post') {
    
    	    $types = array();
    
    	    // Split the post types in array $types
    	    if (!empty($hits)) {
    	        foreach ($hits[0] as $hit) {
    	            if (!is_array($types[$hit->post_type])) $types[$hit->post_type] = array();
    	            array_push($types[$hit->post_type], $hit);
    	        }
    	    }
    
    	 	if (!is_array($types['image'])) $types['image'] = array();
    	 	if (!is_array($types['video'])) $types['video'] = array();
    	 	if (!is_array($types['quote'])) $types['quote'] = array();
    	 	if (!is_array($types['topic'])) $types['topic'] = array();
    	 	if (!is_array($types['post'])) $types['post'] = array();
    
    	    // Merge back to $hits in the desired order
    	    $hits[0] = array_merge($types['video'], $types['image'], $types['quote'], $types['topic'], $types['post']);
    	    return $hits;
        }
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags