• I am trying to prioritize certain search results on my site. I have a custom field called “priorityplace” controlled by a checkbox. What I want to happen is for search results to place results with this item checked at the top of the results with the ones without it following.

    I tried using some of the code on this page: https://www.relevanssi.com/user-manual/relevanssi_hits_filter/

    Here’s my version of the code below which is placed at the bottom of the functions.php file. I’m guessing I did something wrong but I don’t know what. (I’m not very good with PHP yet)

    Thanks in advance.

    —–

    add_filter(‘relevanssi_hits_filter’, ‘order_the_results’);
    function order_the_results($hits) {
    global $wp_query;

    switch ($wp_query->query_vars[‘orderby’]) {
    case ‘likes’:
    $likes = array();
    foreach ($hits[0] as $hit) {
    $likecount = get_post_meta($hit->ID, ‘priorityplace’, true);
    if (!isset($likes[$likecount])) $likes[$likecount] = array();
    array_push($likes[$likecount], $hit);
    }

    if ($wp_query->query_vars[‘order’] == ‘asc’) {
    ksort($likes);
    } else {
    krsort($likes);
    }

    $sorted_hits = array();
    foreach ($likes as $likecount => $year_hits) {
    $sorted_hits = array_merge($sorted_hits, $year_hits);
    }
    $hits[0] = $sorted_hits;
    break;

    case ‘relevance’:
    //do nothing
    break;

    }
    return $hits;
    }

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Tvstuff

    (@tvstuff)

    addendum: The sample page is my search results. “Ocean” should be at the top as it’s the only one with the priority tag checked.

    Plugin Author Mikko Saari

    (@msaari)

    The code you copied as a basis here doesn’t do what you expect it to do, it’s for a completely different scenario. This should work better:

    add_filter( 'relevanssi_hits_filter', 'order_the_results' );
    function order_the_results( $hits ) {
        $priority_posts = array();
        $regular_posts  = array();
        foreach ( $hits[0] as $hit ) {
            $priority = get_post_meta( $hit->ID, ‘priorityplace’, true );
            if ( isset( $priority ) ) {
                 $priority_posts[] = $hit;
            } else {
                 $regular_posts[] = $hit;
            }
        }
        $hits[0]?= array_merge( $priority_posts, $regular_posts ); 
        return $hits;
    }
    Thread Starter Tvstuff

    (@tvstuff)

    Okay, I changed out the old code for this one, but I’m still not getting Ocean first. Am I missing a setting?

    Plugin Author Mikko Saari

    (@msaari)

    Then it’s time to do some debugging.

    After the line

    $priority = get_post_meta( $hit->ID, ‘priorityplace’, true );

    add

    var_dump( $priority );

    and run the search. What does that print out? This should tell whether the code is thinking everything is a priority post or if nothing is. You sure you have the name of the custom field right?

    Thread Starter Tvstuff

    (@tvstuff)

    Yes, I checked. I also know it’s being saved because of a marker I put on the page itself (if page is marked priority, a little red “Priority” appears on the screen under the hours)

    I added your code and ran it and got this:

    string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""

    …which doesn’t look good.

    Plugin Author Mikko Saari

    (@msaari)

    Ok, that means Relevanssi doesn’t get any priority info for any of the posts it finds.

    Change the var_dump( $priority ); to var_dump( $hit->ID );, and you’ll get the post IDs. You can then go check the wp_postmeta database table to see what kind of values the priorityplace custom field is having for those posts.

    Thread Starter Tvstuff

    (@tvstuff)

    The values for priorityplace are 0 or 1 (off or on). The values seem to be matching up with what I have set.

    • This reply was modified 6 years, 9 months ago by Tvstuff.
    Plugin Author Mikko Saari

    (@msaari)

    The code is correct, except that $priority = get_post_meta( $hit->ID, ‘priorityplace’, true ); has curly apostrophes when it should have straight apostrophes: $priority = get_post_meta( $hit->ID, 'priorityplace', true ); Could it be that?

    If changing that doesn’t help, then I’m not sure what more can I do here… that’s how it should work. Loop through the posts in the results, get the meta field value for each post, then split into two arrays based on that and in the end combine the arrays.

    Thread Starter Tvstuff

    (@tvstuff)

    Well it sort of worked. Changing the quotes made the one tagged with Priority appear at the bottom instead of the top, but at least the order changed so that’s progress. ??

    It also includes the home page in results and I don’t want that, but I can worry about that later.

    Plugin Author Mikko Saari

    (@msaari)

    Hmm, the code should get them in the correct order. Is your search results template a regular loop, or can it perhaps flip the order somehow?

    It’s simple to exclude posts from the results. See the “Post exclusion” setting on Searching tab of Relevanssi settings.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Prioritizing search results’ is closed to new replies.