B
B
Boris Belov2016-01-06 12:01:50
WordPress
Boris Belov, 2016-01-06 12:01:50

How to sort wordpress search results?

Good morning.
Such a question: How to sort wordpress search results?
The output of posts is organized through the simplest

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Now the results are displayed in some unknown, random way.
It is necessary at least that it was on DESC.
What is better and how to prescribe and do to achieve the desired result?
---
For yes, WordPress initially renders by date, but not the desired one.
Example 1 and Example 2 .
The first option is correct, but the second is wrong. And the only difference is that the second uses FIRST NAME + LAST NAME, and the first uses LAST NAME.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2016-01-06
@iborisbelov

Somehow so

function my_search_order( $query ) {
    if ($query->is_search) {
  $query->set( 'order', 'DESC' );
        $query->set( 'orderby', 'date' );
    };
    return $query;
};
add_filter( 'pre_get_posts', 'my_search_order' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question