WordPress.org

Ready to get started?Download WordPress

Forums

Relevanssi - A Better Search
[resolved] Give certain pages a higher rank (4 posts)

  1. jeffreyvr
    Member
    Posted 1 year ago #

    Hello everyone,

    I have a page titled "Damian Lewis" and some posts titled "Damian Lewis wins Golden Globe" and "Damian Lewis stars in Homeland".

    I would like to make sure that the "Damian Lewis" page always comes first in the searchresults. I can't seem to get that to work, probable because the prashe "Damian Lewis" is used more in the posts content.

    Is there a workaround for this? Any help would be very much appreciated.

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

  2. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Relevanssi Premium has a feature of adjusting weights by post type, in which case you could boost pages over posts. Otherwise a manual adjustment using either relevanssi_match or relevanssi_hits_filter filter hooks is the way to go.

  3. jeffreyvr
    Member
    Posted 1 year ago #

    Thanks for your reply. I think I almost got it, but only pages are returned now. Can you see why this happens?

    add_filter('relevanssi_hits_filter', 'pages_first');
    function pages_first($hits) {
    
    	$pages = array();
    	$everything_else = array();
    
    	foreach($hits[0] as $hit){
    		$page = false;
    		if($hit->post_type == 'page'){
    			$page = true;
    			break;
    		}
    	}
    
    	$page ? array_push($pages, $hit) : array_push($everything_else, $hit);
    
    	$hits[0] = array_merge($pages, $everything_else);
    	return $hits;
    
    }
  4. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Now the loop goes through the hits, until it finds a page, then puts the page in the $pages array and returns that.

    Remove the break and put the array_push() stuff inside the foreach loop. Then $hits will contain all posts, pages first.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic