Order results witin search.php by custom meta field

I need some help with ordering the results within search.php

If I search for the keyword “XYZ”, by default it returns posts that have “XYZ” within the title, which is great.

Read More

As an example, let say I get the following results:
XYZ1
XYZ2
XYZ3

However, I want to sort those 3 results by a custom meta field called “priority”

so the above should look like this
XYZ2 (has priority 10)
XYZ1 (has priority 7)
XYZ3 (has priority 4)

I tried the following:

<?php
    $args = array(
    'orderby'   => 'meta_value_num',
    'meta_key'  => 'priority',
    'order'     => 'DESC'
);
query_posts($args);
while ( have_posts() ) : the_post();
    // Results
endwhile;
?>

But what I get in return are irrelevant posts ordered by priority, for example
XYZ2 (has priority 10)
ABC3 (has priority 9)
BBA4 (has priority 8)
XYZ1 (has priority 7)
… and so on

No sure if I’m missing something.


I also tried this:

<?php
function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
        $query->set('meta_key', 'priority' );
        $query->set('orderby', 'meta_value_num' );
        $query->set('order', 'DESC' );
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');
?>

Here’s the final code snippet that worked for me.

<?php
function SearchFilter($query) {
    if ($query->is_main_query() && $query->is_search()) {
        $query->set('post_type', 'post');
        $query->set('showposts', -1);
        $query->set('meta_key', 'priority' );
        $query->set('orderby', 'meta_value_num' );
        $query->set('order', 'DESC' );
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
?>

Related posts

1 comment

  1. How about the snippets following?

        if (get_query_var('paged'))
          $paged = get_query_var('paged');
        elseif (get_query_var('page'))
          $paged = get_query_var('page');
        else
          $paged = 1;
    
        $s = get_query_var('s');
    
        $args = array(
            'orderby' => 'meta_value_num',
            'meta_key' => 'priority',
            'order' => 'DESC',
            'paged' => $paged,
            's' => $s,
            'post_type' => array('post'),
        );
    
        query_posts($args);
    

Comments are closed.