L
L
Lecturer2020-09-26 22:38:29
PHP
Lecturer, 2020-09-26 22:38:29

How to display search results for sections in wordpress?

I did a search on the site, in the search results you need not just dump posts, but split them into categories, for example:

In the news: Post1, Post2, Post3
In events: Post5, Post8
In the structure: Post19

Well, and so on, I did not find this in the docks

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Aleksey Ratnikov, 2015-12-01
@mahoho

FIND_IN_SET()
This is at least the third time I've answered a similar question where people can't google this function.

I
IceJOKER, 2015-12-01
@IceJOKER

First, look towards fulltext search (there is a lot of information in the search).
If it doesn't work, you can do something like this -

$words = array('Привет', 'привет', 'Хай', 'хай', 'Хэй', 'Хэллоу');
$impl = implode('%', $words);
$where = "TEXT LIKE '%{$impl}%'";

Or in the same way using OR, i.e. we divide the array into separate words and search through OR

E
Evgeniy Zavyalov, 2015-12-01
@profit

like this

select *
  from <table>
 where upper(text) like upper('%Привет%' )
       or upper(text) like upper('%Хай%' )
       or upper(text) like upper('%Хэй%' )
       or upper(text) like upper('%Хэллоу%' )

but this is just an example. see plan, indexes and...

I
igruschkafox, 2015-12-02
@igruschkafox

There is an array of words (s)
then just
save this array to a temporary table
and join your table with the
PS array table if the array is large - do not forget to make an index for it on the temporary table

I
Ivan Kozlov, 2020-09-27
@trampick

In my opinion, a simple solution would be:
In the search.php template (if it does not exist, you need to create it), in the main while loop, add the data to an array, grouping it according to the necessary criteria (news, events).
How to create grouping conditions is a question for the structure of your site, what types of posts, taxonomies are there. Immediately add the necessary record data to the array - titles, links, etc.
Then, through the loop, output the already grouped data to the template.
There is a question about how to do pagination correctly. I would limit the search result to a certain number of entries, for example 100. And I would display everything on 1 page. This is the easiest option.
There are many options on how to implement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question